Skip to content

Instantly share code, notes, and snippets.

@arvilmena
Last active February 5, 2019 07:06
Show Gist options
  • Save arvilmena/be0709290a8dc04d5a7a072d1b6e337c to your computer and use it in GitHub Desktop.
Save arvilmena/be0709290a8dc04d5a7a072d1b6e337c to your computer and use it in GitHub Desktop.
class CompanyEntity implements EntityInterface {
    
    public function get_entity_type() {
        return 'company';
    }
    
}

class ProductEntity implements EntityInterface {
    
    public function get_entity_type() {
        return 'product';
    }
}

class EntityFactory {
    
    private $company_entity;
    private $product_entity;
    
    public function __construct() {
        $this->company_entity = new CompanyEntity();
        $this->product_entity = new ProductEntity();
    }
    
    public function get_company_entity() {
        return $this->company_entity;
    }
    
    public function get_product_entity() {
        return $this->product_entity;
    }
    
    public function get_object_by_row_id( $entity_id ) {
    
        $row = get_row( $entity_id );
        
        switch( $row->entity_type ) {
            case 'company':
                return $this->product_entity;
                break;
            case 'product':
                return $this->product_entity;
                break;
        }
    }
}

class AdminRole implements RoleInterface {
    
    public function get_role_unique_name {
        return 'administrator';
    }
    
}

class EditorRole implements RoleInterface {
    
    public function get_role_unique_name {
        return 'editor';
    }
    
}

class Actor {
    
    private $entity_factory;
    private $user_object;
    
    private function __construct( $user_id ) {
        $this->entity_factory = new EntityFactory();
        $this->user_object = get_user( $user_id );
    }
    
    public function add_role( RoleInterface $role_object ) {
    
        $inserted_row = $this->user_object->insert(
                'roles_table', 
                $role_object->get_role_unique_name()
            );
            
        return $inserted_row;
    }
    
    public function remove_role( RoleInterface $role_object ) {
    
        $deleted_row = $this->user_object->delete(
                'roles_table', 
                $role_object->get_role_unique_name()
            );
        
        return $deleted_row;
    }
    
    public function get_all_roles() {
        return $this->user_object->get( 'roles_table' )->mapToObject();
    }
    
    /*
     * @var $action string
     * @var $entity_id int
     */
    public function can_do_on_entity_id( $action, $entity_id ) {
        
        $entity_object = $this->entity_factory->get_object_by_row_id( $entity_id );
        $role_objects = $this->get_all_roles();
        
        /**
         *   TODO: How/What to proceed doing here?
         *   
         *   Now I'm confused... Where should I insert the "Roles",
         *   and "Permissions."
         *   
         *   I imagine, given the $action string, I'll do a `in_array()` search
         *   with all the permissions for that Entity. I'm lost.
         *   Where should these `$action`s be defined? in EntityObject?
         *   or in `Permission Class`? what should `Permission Class` do?
         */
        
        
    }
}

class App {

    .
    .
    public static function get_user() {
        $user_id = $_SESSION['user']->id;
        return new Actor( $user_id )
    }
    .
    .
    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment