src/Bidcoz/Bundle/CoreBundle/Entity/Sponsorship/Sponsorship.php line 20

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\CoreBundle\Entity\Sponsorship;
  3. use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
  4. use Bidcoz\Bundle\CoreBundle\Entity\Contact\Company;
  5. use Bidcoz\Bundle\CoreBundle\Entity\Image;
  6. use Bidcoz\Bundle\CoreBundle\Entity\Purchasable;
  7. use Bidcoz\Bundle\CoreBundle\Entity\Purchase\PurchaseUtils;
  8. use Bidcoz\Bundle\CoreBundle\Entity\Purchase\SponsorshipPurchase;
  9. use Bidcoz\Bundle\CoreBundle\Entity\User;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  13. /**
  14.  * @ORM\Entity(repositoryClass="SponsorshipRepository")
  15.  * @ORM\Table(name="sponsorships")
  16.  */
  17. class Sponsorship implements Purchasable
  18. {
  19.     use PurchaseUtils;
  20.     /**
  21.      * @ORM\Id
  22.      * @ORM\Column(name="id", type="integer")
  23.      * @ORM\GeneratedValue(strategy="AUTO")
  24.      */
  25.     protected $id;
  26.     /**
  27.      * @ORM\Column(name="url", type="text", nullable = true)
  28.      * @Assert\Url
  29.      */
  30.     protected $url;
  31.     /**
  32.      * @ORM\Column(name="hits", type="integer")
  33.      */
  34.     protected $hits 0;
  35.     /**
  36.      * @ORM\Column(name="last_hit", type="datetime", nullable=true)
  37.      */
  38.     protected $lastHit;
  39.     /**
  40.      * @ORM\Column(name="active", type="boolean")
  41.      */
  42.     protected $active true;
  43.     /**
  44.      * @ORM\ManyToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Campaign", inversedBy="sponsorships")
  45.      * @ORM\JoinColumn(name="campaign_id", referencedColumnName="id", onDelete="CASCADE")
  46.      */
  47.     protected ?Campaign $campaign null;
  48.     /**
  49.      * @ORM\ManyToOne(targetEntity="SponsorshipLevel", inversedBy="sponsorships", cascade={"persist"})
  50.      * @ORM\JoinColumn(name="level_id", referencedColumnName="id", onDelete="CASCADE")
  51.      */
  52.     protected ?SponsorshipLevel $level null;
  53.     /**
  54.      * @ORM\OneToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Image")
  55.      */
  56.     protected ?Image $image null;
  57.     /**
  58.      * @ORM\ManyToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\User")
  59.      * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  60.      */
  61.     protected ?User $donor null;
  62.     /**
  63.      * @ORM\ManyToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Contact\Company")
  64.      * @ORM\JoinColumn(name="company_id", referencedColumnName="id", onDelete="CASCADE")
  65.      */
  66.     protected ?Company $company null;
  67.     /**
  68.      * @ORM\OneToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Purchase\SponsorshipPurchase", mappedBy="sponsorship")
  69.      */
  70.     protected ?SponsorshipPurchase $purchase null;
  71.     public function __construct(Campaign $campaignSponsorshipLevel $level null)
  72.     {
  73.         $this->campaign $campaign;
  74.         $this->level    $level;
  75.     }
  76.     public function getId()
  77.     {
  78.         return $this->id;
  79.     }
  80.     public function setId($id): void
  81.     {
  82.         $this->id $id;
  83.     }
  84.     public function getCampaign(): ?Campaign
  85.     {
  86.         return $this->campaign;
  87.     }
  88.     public function setCampaign(?Campaign $campaign): void
  89.     {
  90.         $this->campaign $campaign;
  91.     }
  92.     public function setUrl($url)
  93.     {
  94.         $this->url $url;
  95.     }
  96.     public function getUrl()
  97.     {
  98.         return $this->url;
  99.     }
  100.     public function setHits($hits)
  101.     {
  102.         $this->hits $hits;
  103.     }
  104.     public function getHits()
  105.     {
  106.         return $this->hits;
  107.     }
  108.     public function incrementHits()
  109.     {
  110.         ++$this->hits;
  111.     }
  112.     public function setActive($active)
  113.     {
  114.         $this->active = (bool) $active;
  115.     }
  116.     public function isActive()
  117.     {
  118.         return $this->image && $this->active;
  119.     }
  120.     public function isVisible()
  121.     {
  122.         return $this->isActive() && $this->isPurchased();
  123.     }
  124.     public function setImage(?Image $image null): void
  125.     {
  126.         $this->image $image;
  127.     }
  128.     public function getImage(): ?Image
  129.     {
  130.         return $this->image;
  131.     }
  132.     public function setLevel(?SponsorshipLevel $level): void
  133.     {
  134.         $this->level $level;
  135.     }
  136.     public function getLevel(): ?SponsorshipLevel
  137.     {
  138.         return $this->level;
  139.     }
  140.     public function setDonor(?User $donor): void
  141.     {
  142.         $this->donor $donor;
  143.     }
  144.     public function getDonor(): ?User
  145.     {
  146.         return $this->donor;
  147.     }
  148.     public function getPurchaser(): ?User
  149.     {
  150.         return $this->donor;
  151.     }
  152.     public function setCompany(?Company $company): void
  153.     {
  154.         $this->company $company;
  155.     }
  156.     public function getCompany(): ?Company
  157.     {
  158.         return $this->company;
  159.     }
  160.     public function canBeDeleted()
  161.     {
  162.         return !$this->isPurchased();
  163.     }
  164.     public function getPurchasePrice()
  165.     {
  166.         return $this->purchase
  167.             $this->purchase->getAmount()
  168.             : 0;
  169.     }
  170.     /**
  171.      * @Assert\Callback
  172.      */
  173.     public function ensureChoicesValid(ExecutionContextInterface $context)
  174.     {
  175.         if ($this->isActive() && !$this->image) {
  176.             $context->buildViolation('An image is required to make the sponsorship active')
  177.                 ->atPath('image')
  178.                 ->addViolation();
  179.         }
  180.     }
  181. }