<?php
namespace App\Entity;
use App\Repository\StripeTransactionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\WorkspaceRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Workspace
{
public const PAYMENT_NOT_SELECTED = 0;
public const PAYMENT_TYPE_PAYPAL = 1;
public const PAYMENT_TYPE_STRIPE = 2;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="App\Entity\User")
*/
private $owner;
/**
* @ORM\OneToMany(targetEntity="App\Entity\UserGroup", mappedBy="workspace")
*/
private $userGroup;
/**
* @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="workspace")
*/
private $users;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Rollout", mappedBy="workspace")
*/
private $rollouts;
/**
* @ORM\OneToMany(targetEntity="App\Entity\DeviceGroup", mappedBy="workspace")
*/
private $deviceGroups;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Device", mappedBy="workspace")
*/
private $devices;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Invoice", mappedBy="workspace")
*/
private $invoices;
/**
* @ORM\OneToMany(targetEntity="App\Entity\FileGroup", mappedBy="workspace")
*/
private $fileGroups;
/**
* @ORM\OneToMany(targetEntity="App\Entity\File", mappedBy="workspace")
*/
private $files;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Support", mappedBy="workspace")
*/
private $supports;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $customerKey;
/**
* @ORM\OneToMany(targetEntity="App\Entity\DeviceKey", mappedBy="workspace")
*/
private $deviceKeys;
/**
* @ORM\OneToMany(targetEntity="App\Entity\SandboxRollout", mappedBy="workspace")
*/
private $sandboxRollouts;
/**
* @ORM\OneToMany(targetEntity="App\Entity\SandboxDeviceGroup", mappedBy="workspace")
*/
private $sandboxDeviceGroups;
/**
* @ORM\OneToMany(targetEntity="App\Entity\SandboxDevice", mappedBy="workspace")
*/
private $sandboxDevices;
/**
* @ORM\OneToMany(targetEntity="App\Entity\SandboxFileGroup", mappedBy="workspace")
*/
private $sandboxFileGroups;
/**
* @ORM\OneToMany(targetEntity="App\Entity\SandboxFile", mappedBy="workspace")
*/
private $sandboxFiles;
/**
* @ORM\Column(type="boolean", options={"default" : false}))
*/
private $disabled = false;
/**
* @ORM\Column(type="boolean", options={"default" : false}))
*/
private $deleted = false;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Plan")
* @ORM\JoinTable(name="workspace_plan",
* joinColumns={@ORM\JoinColumn(name="workspace_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="plan_id", referencedColumnName="id")}
* )
*/
private $plans;
/**
* @ORM\OneToMany(targetEntity=PayPalTransaction::class, mappedBy="workspace", orphanRemoval=true)
*/
private $payPalTransactions;
/**
* @ORM\OneToMany(targetEntity=StripeTransaction::class, mappedBy="workspace", orphanRemoval=true)
*/
private $stripeTransactions;
/**
* @ORM\OneToMany(targetEntity=PlanChange::class, mappedBy="workspace", orphanRemoval=true)
*/
private $planChanges;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastAvailableUpdatesMailSent = null;
/**
* @ORM\Column(type="integer", options={"default": 0}))
*/
private $paymentType = 0;
public function __construct()
{
$this->userGroup = new ArrayCollection();
$this->users = new ArrayCollection();
$this->rollouts = new ArrayCollection();
$this->deviceGroups = new ArrayCollection();
$this->devices = new ArrayCollection();
$this->invoices = new ArrayCollection();
$this->fileGroups = new ArrayCollection();
$this->files = new ArrayCollection();
$this->supports = new ArrayCollection();
$this->sandboxRollouts = new ArrayCollection();
$this->sandboxDeviceGroups = new ArrayCollection();
$this->sandboxDevices = new ArrayCollection();
$this->sandboxFileGroups = new ArrayCollection();
$this->sandboxFiles = new ArrayCollection();
$this->plans = new ArrayCollection();
$this->payPalTransactions = new ArrayCollection();
$this->planChanges = new ArrayCollection();
$this->deviceKeys = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): self
{
$this->owner = $owner;
return $this;
}
/**
* @return Collection|UserGroup[]
*/
public function getUserGroup(): Collection
{
return $this->userGroup;
}
public function addUserGroup(UserGroup $userGroup): self
{
if (!$this->userGroup->contains($userGroup)) {
$this->userGroup[] = $userGroup;
$userGroup->setWorkspace($this);
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): self
{
if ($this->userGroup->contains($userGroup)) {
$this->userGroup->removeElement($userGroup);
// set the owning side to null (unless already changed)
if ($userGroup->getWorkspace() === $this) {
$userGroup->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users->add($user);
$user->setWorkspace($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->contains($user)) {
$this->users->removeElement($user);
// set the owning side to null (unless already changed)
if ($user->getWorkspace() === $this) {
$user->setWorkspace(null);
}
}
return $this;
}
/**
* @return int
*/
public function getAvailableUpdates(): int
{
if (is_object($this->getPlan())) {
$allowed = $this->getPlan()->getAllowedDevices();
$countDevices = count($this->getFilteredUsedDevices());
return ((($allowed - $countDevices) < 0) ? 0 : ($allowed - $countDevices));
}
return 0;
}
/**
* @return int
*/
public function getActiveDevices(): int
{
return count($this->getFilteredUsedDevices());
}
/**
* @return int
*/
public function getUsedDevices(): int
{
if (is_object($this->getPlan())) {
return count($this->getFilteredUsedDevices());
}
return 0;
}
/**
* @return Collection
*/
private function getFilteredUsedDevices(): Collection
{
return $this->getDevices()->filter(function (Device $device) {
return (
($device->getBlackListed() === false && $device->getTransactionId() !== null)
or
($device->getBlackListed() === false && $device->getDeviceGroup() !== null)
);
});
}
/**
* @return Collection|Rollout[]
*/
public function getRollouts(): Collection
{
return $this->rollouts;
}
/**
* @param Rollout $rollout
* @return $this
*/
public function addRollout(Rollout $rollout): self
{
if (!$this->rollouts->contains($rollout)) {
$this->rollouts->add($rollout);
$rollout->setWorkspace($this);
}
return $this;
}
public function removeRollout(Rollout $rollout): self
{
if ($this->rollouts->contains($rollout)) {
$this->rollouts->removeElement($rollout);
// set the owning side to null (unless already changed)
if ($rollout->getWorkspace() === $this) {
$rollout->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|DeviceGroup[]
*/
public function getDeviceGroups(): Collection
{
return $this->deviceGroups;
}
/**
* @param DeviceGroup $deviceGroup
* @return $this
*/
public function addDeviceGroup(DeviceGroup $deviceGroup): self
{
if (!$this->deviceGroups->contains($deviceGroup)) {
$this->deviceGroups->add($deviceGroup);
$deviceGroup->setWorkspace($this);
}
return $this;
}
/**
* @param DeviceGroup $deviceGroup
* @return $this
*/
public function removeDeviceGroup(DeviceGroup $deviceGroup): self
{
if ($this->deviceGroups->contains($deviceGroup)) {
$this->deviceGroups->removeElement($deviceGroup);
if ($deviceGroup->getWorkspace() === $this) {
$deviceGroup->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|Device[]
*/
public function getDevices(): Collection
{
return $this->devices;
}
/**
* @param Device $device
* @return $this
*/
public function addDevice(Device $device): self
{
if (!$this->devices->contains($device)) {
$this->devices->add($device);
$device->setWorkspace($this);
}
return $this;
}
/**
* @param Device $device
* @return $this
*/
public function removeDevice(Device $device): self
{
if ($this->devices->contains($device)) {
$this->devices->removeElement($device);
if ($device->getWorkspace() === $this) {
$device->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|FileGroup[]
*/
public function getFileGroups(): Collection
{
return $this->fileGroups;
}
/**
* @param FileGroup $fileGroup
* @return $this
*/
public function addFileGroup(FileGroup $fileGroup): self
{
if (!$this->fileGroups->contains($fileGroup)) {
$this->fileGroups->add($fileGroup);
$fileGroup->setWorkspace($this);
}
return $this;
}
/**
* @param FileGroup $fileGroup
* @return $this
*/
public function removeFileGroup(FileGroup $fileGroup): self
{
if ($this->fileGroups->contains($fileGroup)) {
$this->fileGroups->removeElement($fileGroup);
if ($fileGroup->getWorkspace() === $this) {
$fileGroup->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|File[]
*/
public function getFiles(): Collection
{
return $this->files;
}
/**
* @param File $file
* @return $this
*/
public function addFile(File $file): self
{
if (!$this->files->contains($file)) {
$this->files->add($file);
$file->setWorkspace($this);
}
return $this;
}
/**
* @param File $file
* @return $this
*/
public function removeFile(File $file): self
{
if ($this->files->contains($file)) {
$this->files->removeElement($file);
if ($file->getWorkspace() === $this) {
$file->setWorkspace(null);
}
}
return $this;
}
public function getCustomerKey(): ?string
{
return $this->customerKey;
}
public function setCustomerKey(?string $customerKey): self
{
$this->customerKey = $customerKey;
return $this;
}
public function getDeviceKey(): ?string
{
if (count($this->deviceKeys) > 0) {
/** @var DeviceKey $deviceKey */
$deviceKey = $this->deviceKeys->first();
return $deviceKey->getDeviceKey();
}
return null;
}
/**
* @return Collection|Support[]
*/
public function getSupports(): Collection
{
return $this->supports;
}
/**
* @param Support $support
* @return $this
*/
public function addSupport(Support $support): self
{
if (!$this->supports->contains($support)) {
$this->supports->add($support);
$support->setWorkspace($this);
}
return $this;
}
/**
* @param Support $support
* @return $this
*/
public function removeSupport(Support $support): self
{
if ($this->supports->contains($support)) {
$this->supports->removeElement($support);
if ($support->getWorkspace() === $this) {
$support->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|SandboxRollout[]
*/
public function getSandboxRollouts(): Collection
{
return $this->sandboxRollouts;
}
/**
* @param SandboxRollout $sandboxRollout
* @return $this
*/
public function addSandboxRollout(SandboxRollout $sandboxRollout): self
{
if (!$this->sandboxRollouts->contains($sandboxRollout)) {
$this->sandboxRollouts->add($sandboxRollout);
$sandboxRollout->setWorkspace($this);
}
return $this;
}
/**
* @param SandboxRollout $sandboxRollout
* @return $this
*/
public function removeSandboxRollout(SandboxRollout $sandboxRollout): self
{
if ($this->sandboxRollouts->contains($sandboxRollout)) {
$this->sandboxRollouts->removeElement($sandboxRollout);
// set the owning side to null (unless already changed)
if ($sandboxRollout->getWorkspace() === $this) {
$sandboxRollout->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|SandboxDeviceGroup[]
*/
public function getSandboxDeviceGroups(): Collection
{
return $this->sandboxDeviceGroups;
}
/**
* @param SandboxDeviceGroup $sandboxDeviceGroup
* @return $this
*/
public function addSandboxDeviceGroup(SandboxDeviceGroup $sandboxDeviceGroup): self
{
if (!$this->sandboxDeviceGroups->contains($sandboxDeviceGroup)) {
$this->sandboxDeviceGroups->add($sandboxDeviceGroup);
$sandboxDeviceGroup->setWorkspace($this);
}
return $this;
}
/**
* @param SandboxDeviceGroup $sandboxDeviceGroup
* @return $this
*/
public function removeSandboxDeviceGroup(SandboxDeviceGroup $sandboxDeviceGroup): self
{
if ($this->sandboxDeviceGroups->contains($sandboxDeviceGroup)) {
$this->sandboxDeviceGroups->removeElement($sandboxDeviceGroup);
if ($sandboxDeviceGroup->getWorkspace() === $this) {
$sandboxDeviceGroup->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|SandboxDeviceGroup[]
*/
public function getSandboxDevices(): Collection
{
return $this->sandboxDevices;
}
/**
* @param SandboxDevice $sandboxDevice
* @return $this
*/
public function addSandboxDevice(SandboxDevice $sandboxDevice): self
{
if (!$this->sandboxDevices->contains($sandboxDevice)) {
$this->sandboxDevices->add($sandboxDevice);
$sandboxDevice->setWorkspace($this);
}
return $this;
}
/**
* @param SandboxDevice $sandboxDevice
* @return $this
*/
public function removeSandboxDevice(SandboxDevice $sandboxDevice): self
{
if ($this->sandboxDevices->contains($sandboxDevice)) {
$this->sandboxDevices->removeElement($sandboxDevice);
if ($sandboxDevice->getWorkspace() === $this) {
$sandboxDevice->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|SandboxFileGroup[]
*/
public function getSandboxFileGroups(): Collection
{
return $this->sandboxFileGroups;
}
/**
* @param SandboxFileGroup $sandboxFileGroup
* @return $this
*/
public function addSandboxFileGroup(SandboxFileGroup $sandboxFileGroup): self
{
if (!$this->sandboxFileGroups->contains($sandboxFileGroup)) {
$this->sandboxFileGroups->add($sandboxFileGroup);
$sandboxFileGroup->setWorkspace($this);
}
return $this;
}
/**
* @param SandboxFileGroup $sandboxFileGroup
* @return $this
*/
public function removeSandboxFileGroup(SandboxFileGroup $sandboxFileGroup): self
{
if ($this->sandboxFileGroups->contains($sandboxFileGroup)) {
$this->sandboxFileGroups->removeElement($sandboxFileGroup);
if ($sandboxFileGroup->getWorkspace() === $this) {
$sandboxFileGroup->setWorkspace(null);
}
}
return $this;
}
/**
* @return Collection|SandboxFile[]
*/
public function getSandboxFiles(): Collection
{
return $this->sandboxFiles;
}
/**
* @param SandboxFile $sandboxFile
* @return $this
*/
public function addSandboxFile(SandboxFile $sandboxFile): self
{
if (!$this->sandboxFiles->contains($sandboxFile)) {
$this->sandboxFiles->add($sandboxFile);
$sandboxFile->setWorkspace($this);
}
return $this;
}
/**
* @param SandboxFile $sandboxFile
* @return $this
*/
public function removeSandboxFile(SandboxFile $sandboxFile): self
{
if ($this->sandboxFiles->contains($sandboxFile)) {
$this->sandboxFiles->removeElement($sandboxFile);
if ($sandboxFile->getWorkspace() === $this) {
$sandboxFile->setWorkspace(null);
}
}
return $this;
}
/**
* @return bool
*/
public function isDisabled(): bool
{
return $this->disabled;
}
/**
* @param bool $disabled
*/
public function setDisabled(bool $disabled): void
{
$this->disabled = $disabled;
}
/**
* @return Plan|null
*/
public function getPlan(): ?Plan
{
if (count($this->plans) > 0) {
return $this->plans->first();
}
return null;
}
/**
* @param Plan $plan
* @return $this
*/
public function setPlan(Plan $plan): self
{
if (!$this->plans->contains($plan)) {
$this->plans->clear();
$this->plans->add($plan);
}
return $this;
}
/**
* @return Collection|PayPalTransaction[]
*/
public function getPayPalTransactions(): Collection
{
return $this->payPalTransactions;
}
public function addPayPalTransaction(PayPalTransaction $payPalTransaction): self
{
if (!$this->payPalTransactions->contains($payPalTransaction)) {
$this->payPalTransactions[] = $payPalTransaction;
$payPalTransaction->setWorkspace($this);
}
return $this;
}
public function removePayPalTransaction(PayPalTransaction $payPalTransaction): self
{
if ($this->payPalTransactions->removeElement($payPalTransaction)) {
// set the owning side to null (unless already changed)
if ($payPalTransaction->getWorkspace() === $this) {
$payPalTransaction->setWorkspace(null);
}
}
return $this;
}
/**
* @param string|null $planId
* @return bool
*/
public function hasAuthorization(?string $planId): bool
{
if ($this->getPaymentType() == Workspace::PAYMENT_TYPE_PAYPAL) {
/** @var PayPalTransaction $transaction */
foreach ($this->payPalTransactions as $transaction) {
if (
$transaction->getStatus() === PayPalTransaction::STATUS_SUCCESS
&& $transaction->getType() === PayPalTransaction::AUTHORIZE &&
$transaction->getPayPalPlanId() === $planId
&&
!empty($transaction->getAuthId())
) {
return true;
}
}
} elseif ($this->getPaymentType() == Workspace::PAYMENT_TYPE_STRIPE) {
/** @var StripeTransaction $transaction */
foreach ($this->stripeTransactions as $transaction) {
if (
$transaction->getStatus() === StripeTransaction::STATUS_SUCCESS
&& $transaction->getType() === StripeTransaction::AUTHORIZE &&
$transaction->getStripePlanId() === $planId
&&
!empty($transaction->getSessionId())
) {
return true;
}
}
}
return false;
}
/**
* @param string|null $planId
* @return PayPalTransaction|StripeTransaction|null
*/
public function getActiveAuthorization(?string $planId)
{
if ($this->getPaymentType() == Workspace::PAYMENT_TYPE_PAYPAL) {
/** @var PayPalTransaction $transaction */
foreach ($this->payPalTransactions as $transaction) {
if (
$transaction->getStatus() === PayPalTransaction::STATUS_SUCCESS
&& $transaction->getType() === PayPalTransaction::AUTHORIZE &&
$transaction->getPayPalPlanId() === $planId
&&
!empty($transaction->getAuthId())
) {
return $transaction;
}
}
} elseif ($this->getPaymentType() == Workspace::PAYMENT_TYPE_STRIPE) {
/** @var StripeTransaction $transaction */
foreach ($this->stripeTransactions as $transaction) {
if (
$transaction->getStatus() === StripeTransaction::STATUS_SUCCESS
&& $transaction->getType() === StripeTransaction::AUTHORIZE &&
$transaction->getStripePlanId() === $planId
&&
!empty($transaction->getSessionId())
) {
return $transaction;
}
}
}
return null;
}
/**
* @param string|null $payPalPlanId
* @return PayPalTransaction|null
*/
public function getPendingAuthorization(?string $payPalPlanId): ?PayPalTransaction
{
/** @var PayPalTransaction $transaction */
foreach ($this->payPalTransactions as $transaction) {
if (
$transaction->getStatus() === PayPalTransaction::STATUS_PENDING
&& $transaction->getType() === PayPalTransaction::AUTHORIZE &&
$transaction->getPayPalPlanId() === $payPalPlanId &&
!empty($transaction->getAuthId())
) {
return $transaction;
}
}
return null;
}
/**
* @return Collection|PlanChange[]
*/
public function getPlanChanges(): Collection
{
return $this->planChanges;
}
public function addPlanChange(PlanChange $planChange): self
{
if (!$this->planChanges->contains($planChange)) {
$this->planChanges[] = $planChange;
$planChange->setWorkspace($this);
}
return $this;
}
public function removePlanChange(PlanChange $planChange): self
{
if ($this->planChanges->removeElement($planChange)) {
// set the owning side to null (unless already changed)
if ($planChange->getWorkspace() === $this) {
$planChange->setWorkspace(null);
}
}
return $this;
}
/**
* @return bool
*/
public function hasOpenedPlanChange(): bool
{
/** @var PlanChange $planChange */
foreach ($this->planChanges as $planChange) {
if ($planChange->getClosed() === false) {
return true;
}
}
return false;
}
/**
* @return PlanChange|null
*/
public function getOpenedPlanChange(): ?PlanChange
{
/** @var PlanChange $planChange */
foreach ($this->planChanges as $planChange) {
if ($planChange->getClosed() === false) {
return $planChange;
}
}
return null;
}
/**
* @return Collection|Invoice[]
*/
public function getInvoices(): Collection
{
return $this->invoices;
}
/**
* @param Invoice $invoice
* @return $this
*/
public function addInvoice(Invoice $invoice): self
{
if (!$this->invoices->contains($invoice)) {
$this->invoices->add($invoice);
$invoice->setWorkspace($this);
}
return $this;
}
/**
* @param Invoice $invoice
* @return $this
*/
public function removeInvoice(Invoice $invoice): self
{
if ($this->invoices->contains($invoice)) {
$this->invoices->removeElement($invoice);
if ($invoice->getWorkspace() === $this) {
$invoice->setWorkspace(null);
}
}
return $this;
}
/**
* @return bool
*/
public function isDeleted(): bool
{
return $this->deleted;
}
/**
* @param bool $deleted
*/
public function setDeleted(bool $deleted): void
{
$this->deleted = $deleted;
}
/**
* @return null
*/
public function getLastAvailableUpdatesMailSent()
{
return $this->lastAvailableUpdatesMailSent;
}
/**
* @param null $lastAvailableUpdatesMailSent
*/
public function setLastAvailableUpdatesMailSent($lastAvailableUpdatesMailSent): void
{
$this->lastAvailableUpdatesMailSent = $lastAvailableUpdatesMailSent;
}
/**
* @return Collection|DeviceKey[]
*/
public function getDeviceKeys(): Collection
{
return $this->deviceKeys;
}
/**
* @param DeviceKey $deviceKey
* @return $this
*/
public function addDeviceKey(DeviceKey $deviceKey): self
{
if (!$this->deviceKeys->contains($deviceKey)) {
$this->deviceKeys->add($deviceKey);
$deviceKey->setWorkspace($this);
}
return $this;
}
/**
* @param DeviceKey $deviceKey
* @return $this
*/
public function removeDeviceKey(DeviceKey $deviceKey): self
{
if ($this->deviceKeys->contains($deviceKey)) {
$this->deviceKeys->removeElement($deviceKey);
}
return $this;
}
/**
* @return mixed
*/
public function getStripeTransactions()
{
return $this->stripeTransactions;
}
/**
* @param mixed $stripeTransactions
*/
public function setStripeTransactions($stripeTransactions): void
{
$this->stripeTransactions = $stripeTransactions;
}
/**
* @return mixed
*/
public function getPaymentType()
{
return $this->paymentType;
}
/**
* @param mixed $paymentType
*/
public function setPaymentType($paymentType): void
{
$this->paymentType = $paymentType;
}
}