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.     /**
  117.      * A "name-only" sponsor is one whose level uses the Base Display Rate
  118.      * (slots = 1). Base sponsors are displayed as text only — no logo/card —
  119.      * so they do NOT require an image to be active or visible. Any image that
  120.      * happens to be attached to a Base sponsor is ignored for display.
  121.      */
  122.     public function isNameOnly(): bool
  123.     {
  124.         return $this->level && === (int) $this->level->getSlots();
  125.     }
  126.     public function isActive()
  127.     {
  128.         // Image-tier sponsors need a logo to be active; Base (name-only)
  129.         // sponsors are active on the strength of the `active` flag alone.
  130.         return $this->active && ($this->image || $this->isNameOnly());
  131.     }
  132.     public function isVisible()
  133.     {
  134.         return $this->isActive() && $this->isPurchased();
  135.     }
  136.     public function setImage(?Image $image null): void
  137.     {
  138.         $this->image $image;
  139.     }
  140.     public function getImage(): ?Image
  141.     {
  142.         return $this->image;
  143.     }
  144.     public function setLevel(?SponsorshipLevel $level): void
  145.     {
  146.         $this->level $level;
  147.     }
  148.     public function getLevel(): ?SponsorshipLevel
  149.     {
  150.         return $this->level;
  151.     }
  152.     public function setDonor(?User $donor): void
  153.     {
  154.         $this->donor $donor;
  155.     }
  156.     public function getDonor(): ?User
  157.     {
  158.         return $this->donor;
  159.     }
  160.     public function getPurchaser(): ?User
  161.     {
  162.         return $this->donor;
  163.     }
  164.     public function setCompany(?Company $company): void
  165.     {
  166.         $this->company $company;
  167.     }
  168.     public function getCompany(): ?Company
  169.     {
  170.         return $this->company;
  171.     }
  172.     public function canBeDeleted()
  173.     {
  174.         return !$this->isPurchased();
  175.     }
  176.     public function getPurchasePrice()
  177.     {
  178.         return $this->purchase
  179.             $this->purchase->getAmount()
  180.             : 0;
  181.     }
  182.     /**
  183.      * @Assert\Callback
  184.      */
  185.     public function ensureChoicesValid(ExecutionContextInterface $context)
  186.     {
  187.         if ($this->isActive() && !$this->image && !$this->isNameOnly()) {
  188.             $context->buildViolation('An image is required to make the sponsorship active')
  189.                 ->atPath('image')
  190.                 ->addViolation();
  191.         }
  192.     }
  193. }