<?php
namespace Bidcoz\Bundle\CoreBundle\Entity\Sponsorship;
use Bidcoz\Bundle\CoreBundle\Entity\Campaign;
use Bidcoz\Bundle\CoreBundle\Entity\Contact\Company;
use Bidcoz\Bundle\CoreBundle\Entity\Image;
use Bidcoz\Bundle\CoreBundle\Entity\Purchasable;
use Bidcoz\Bundle\CoreBundle\Entity\Purchase\PurchaseUtils;
use Bidcoz\Bundle\CoreBundle\Entity\Purchase\SponsorshipPurchase;
use Bidcoz\Bundle\CoreBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* @ORM\Entity(repositoryClass="SponsorshipRepository")
* @ORM\Table(name="sponsorships")
*/
class Sponsorship implements Purchasable
{
use PurchaseUtils;
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="url", type="text", nullable = true)
* @Assert\Url
*/
protected $url;
/**
* @ORM\Column(name="hits", type="integer")
*/
protected $hits = 0;
/**
* @ORM\Column(name="last_hit", type="datetime", nullable=true)
*/
protected $lastHit;
/**
* @ORM\Column(name="active", type="boolean")
*/
protected $active = true;
/**
* @ORM\ManyToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Campaign", inversedBy="sponsorships")
* @ORM\JoinColumn(name="campaign_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected ?Campaign $campaign = null;
/**
* @ORM\ManyToOne(targetEntity="SponsorshipLevel", inversedBy="sponsorships", cascade={"persist"})
* @ORM\JoinColumn(name="level_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected ?SponsorshipLevel $level = null;
/**
* @ORM\OneToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Image")
*/
protected ?Image $image = null;
/**
* @ORM\ManyToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected ?User $donor = null;
/**
* @ORM\ManyToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Contact\Company")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected ?Company $company = null;
/**
* @ORM\OneToOne(targetEntity="Bidcoz\Bundle\CoreBundle\Entity\Purchase\SponsorshipPurchase", mappedBy="sponsorship")
*/
protected ?SponsorshipPurchase $purchase = null;
public function __construct(Campaign $campaign, SponsorshipLevel $level = null)
{
$this->campaign = $campaign;
$this->level = $level;
}
public function getId()
{
return $this->id;
}
public function setId($id): void
{
$this->id = $id;
}
public function getCampaign(): ?Campaign
{
return $this->campaign;
}
public function setCampaign(?Campaign $campaign): void
{
$this->campaign = $campaign;
}
public function setUrl($url)
{
$this->url = $url;
}
public function getUrl()
{
return $this->url;
}
public function setHits($hits)
{
$this->hits = $hits;
}
public function getHits()
{
return $this->hits;
}
public function incrementHits()
{
++$this->hits;
}
public function setActive($active)
{
$this->active = (bool) $active;
}
public function isActive()
{
return $this->image && $this->active;
}
public function isVisible()
{
return $this->isActive() && $this->isPurchased();
}
public function setImage(?Image $image = null): void
{
$this->image = $image;
}
public function getImage(): ?Image
{
return $this->image;
}
public function setLevel(?SponsorshipLevel $level): void
{
$this->level = $level;
}
public function getLevel(): ?SponsorshipLevel
{
return $this->level;
}
public function setDonor(?User $donor): void
{
$this->donor = $donor;
}
public function getDonor(): ?User
{
return $this->donor;
}
public function getPurchaser(): ?User
{
return $this->donor;
}
public function setCompany(?Company $company): void
{
$this->company = $company;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function canBeDeleted()
{
return !$this->isPurchased();
}
public function getPurchasePrice()
{
return $this->purchase
? $this->purchase->getAmount()
: 0;
}
/**
* @Assert\Callback
*/
public function ensureChoicesValid(ExecutionContextInterface $context)
{
if ($this->isActive() && !$this->image) {
$context->buildViolation('An image is required to make the sponsorship active')
->atPath('image')
->addViolation();
}
}
}