<commit_before><?php

namespace MapasCulturais\Entities;

use Doctrine\ORM\Mapping as ORM;
use MapasCulturais\App;
use MapasCulturais\Traits;

/**
 * Request
 *
 * @property \MapasCulturais\Entities\User $requesterUser
 *
 * @property \MapasCulturais\Entity $origin The origin entity of the requested action
 * @property \MapasCulturais\Entity $destination The destination entity of the requested action
 * @property-read string $requestType The request type
 *
 * @ORM\Table(name="request")
 * @ORM\Entity
 * @ORM\entity(repositoryClass="MapasCulturais\Repository")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
        "ChangeOwnerchip"   = "\MapasCulturais\Entities\RequestChangeOwnership",
        "EventOccurrence"   = "\MapasCulturais\Entities\RequestEventOccurrence",
        "EventProject"      = "\MapasCulturais\Entities\RequestEventProject",
        "ChildEntity"       = "\MapasCulturais\Entities\RequestChildEntity",
        "AgentRelation"     = "\MapasCulturais\Entities\RequestAgentRelation",
        "SealRelation"      = "\MapasCulturais\Entities\RequestSealRelation"
   })
 * @ORM\HasLifecycleCallbacks
 */
abstract class Request extends \MapasCulturais\Entity{
    use Traits\EntityPermissionCache;
    
    const STATUS_PENDING = 1;
    const STATUS_APPROVED = 2;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="request_id_seq", allocationSize=1, initialValue=1)
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="request_uid", type="string", length=32, nullable=false)
     */
    protected $requestUid;

    /**
     * @var string
     *
     * @ORM\Column(name="origin_type", type="string", length=255, nullable=false)
     */
    protected $originType;

    /**
     * @var integer
     *
     * @ORM\Column(name="origin_id", type="integer", nullable=false)
     */
    protected $originId;

    /**
     * @var string
     *
     * @ORM\Column(name="destination_type", type="string", length=255, nullable=false)
     */
    protected $destinationType;

    /**
     * @var integer
     *
     * @ORM\Column(name="destination_id", type="integer", nullable=false)
     */
    protected $destinationId;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_timestamp", type="datetime", nullable=false)
     */
    protected $createTimestamp = 'now()';

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="action_timestamp", type="datetime", nullable=true)
     */
    protected $actionTimestamp;

    /**
     * @var integer
     *
     * @ORM\Column(name="status", type="smallint", nullable=false)
     */
    protected $status = self::STATUS_PENDING;

    /**
     * @var \MapasCulturais\Entities\User
     *
     * @ORM\ManyToOne(targetEntity="MapasCulturais\Entities\User")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="requester_user_id", referencedColumnName="id")
     * })
     */
    protected $requesterUser;


    /**
     *
     * @var \MapasCulturais\Entities\Notification[] Notifications
     * @ORM\OneToMany(targetEntity="MapasCulturais\Entities\Notification", mappedBy="request", cascade="all", orphanRemoval=true)
     */
    protected $notifications;
    
    
    /**
     * @ORM\OneToMany(targetEntity="MapasCulturais\Entities\RequestPermissionCache", mappedBy="owner", cascade="remove", orphanRemoval=true, fetch="EXTRA_LAZY")
     */
    protected $__permissionsCache;

    /**
     * @var string
     *
     * @ORM\Column(name="metadata", type="array", nullable=true)
     */
    protected $metadata = [];


    private $_origin = null;

    private $_destination = null;

    public function __construct() {
        $this->requesterUser = App::i()->user;
        parent::__construct();
    }
    
    function getExtraPermissionCacheUsers(){
        $origin_users = $this->getOrigin()->getUsersWithControl();
        $destination_users = $this->getDestination()->getUsersWithControl();

        return array_merge($origin_users, $destination_users);
    }

    function getOwnerUser() {
        return $this->requesterUser;
    }

    function setOrigin(\MapasCulturais\Entity $entity){
        $this->_origin = $entity;

        $this->originType = $entity->getClassName();
        $this->originId   = $entity->id;
    }

    function getOrigin(){
        if($this->_origin)
            return $this->_origin;
        else
            return App::i()->repo($this->originType)->find($this->originId);
    }
<commit_msg>verify if request origin and destination exists to prevent error in Request class<commit_after><?php

namespace MapasCulturais\Entities;

use Doctrine\ORM\Mapping as ORM;
use MapasCulturais\App;
use MapasCulturais\Traits;

/**
 * Request
 *
 * @property \MapasCulturais\Entities\User $requesterUser
 *
 * @property \MapasCulturais\Entity $origin The origin entity of the requested action
 * @property \MapasCulturais\Entity $destination The destination entity of the requested action
 * @property-read string $requestType The request type
 *
 * @ORM\Table(name="request")
 * @ORM\Entity
 * @ORM\entity(repositoryClass="MapasCulturais\Repository")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({
        "ChangeOwnerchip"   = "\MapasCulturais\Entities\RequestChangeOwnership",
        "EventOccurrence"   = "\MapasCulturais\Entities\RequestEventOccurrence",
        "EventProject"      = "\MapasCulturais\Entities\RequestEventProject",
        "ChildEntity"       = "\MapasCulturais\Entities\RequestChildEntity",
        "AgentRelation"     = "\MapasCulturais\Entities\RequestAgentRelation",
        "SealRelation"      = "\MapasCulturais\Entities\RequestSealRelation"
   })
 * @ORM\HasLifecycleCallbacks
 */
abstract class Request extends \MapasCulturais\Entity{
    use Traits\EntityPermissionCache;
    
    const STATUS_PENDING = 1;
    const STATUS_APPROVED = 2;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     * @ORM\SequenceGenerator(sequenceName="request_id_seq", allocationSize=1, initialValue=1)
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="request_uid", type="string", length=32, nullable=false)
     */
    protected $requestUid;

    /**
     * @var string
     *
     * @ORM\Column(name="origin_type", type="string", length=255, nullable=false)
     */
    protected $originType;

    /**
     * @var integer
     *
     * @ORM\Column(name="origin_id", type="integer", nullable=false)
     */
    protected $originId;

    /**
     * @var string
     *
     * @ORM\Column(name="destination_type", type="string", length=255, nullable=false)
     */
    protected $destinationType;

    /**
     * @var integer
     *
     * @ORM\Column(name="destination_id", type="integer", nullable=false)
     */
    protected $destinationId;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_timestamp", type="datetime", nullable=false)
     */
    protected $createTimestamp = 'now()';

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="action_timestamp", type="datetime", nullable=true)
     */
    protected $actionTimestamp;

    /**
     * @var integer
     *
     * @ORM\Column(name="status", type="smallint", nullable=false)
     */
    protected $status = self::STATUS_PENDING;

    /**
     * @var \MapasCulturais\Entities\User
     *
     * @ORM\ManyToOne(targetEntity="MapasCulturais\Entities\User")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="requester_user_id", referencedColumnName="id")
     * })
     */
    protected $requesterUser;


    /**
     *
     * @var \MapasCulturais\Entities\Notification[] Notifications
     * @ORM\OneToMany(targetEntity="MapasCulturais\Entities\Notification", mappedBy="request", cascade="all", orphanRemoval=true)
     */
    protected $notifications;
    
    
    /**
     * @ORM\OneToMany(targetEntity="MapasCulturais\Entities\RequestPermissionCache", mappedBy="owner", cascade="remove", orphanRemoval=true, fetch="EXTRA_LAZY")
     */
    protected $__permissionsCache;

    /**
     * @var string
     *
     * @ORM\Column(name="metadata", type="array", nullable=true)
     */
    protected $metadata = [];


    private $_origin = null;

    private $_destination = null;

    public function __construct() {
        $this->requesterUser = App::i()->user;
        parent::__construct();
    }
    
    function getExtraPermissionCacheUsers(){
        if($origin = $this->getOrigin()){
            $origin_users = $origin->getUsersWithControl();
        } else {
            $origin_users = [];
        }

        if($destination = $this->getDestination()){
            $destination_users = $this->getDestination()->getUsersWithControl();
        } else {
            $destination_users = [];
        }

        return array_merge($origin_users, $destination_users);
    }

    function getOwnerUser() {
        return $this->requesterUser;
    }

    function setOrigin(\MapasCulturais\Entity $entity){
        $this->_origin = $entity;

        $this->originType = $entity->getClassName();
        $this->originId   = $entity->id;
    }

    function getOrigin(){
        if($this->_origin)
            return $this->_origin;
        else
            return App::i()->repo($this->originType)->find($this->originId);
    }

{'fraction_non_alphanumeric': 0.1439292690449265, 'fraction_numerical': 0.0024673588979130254, 'mean_word_length': 2.9917931883463273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2561015', 'n_tokens_mistral': 2999, 'n_tokens_neox': 3002, 'n_words': 462}