Skip to content
Snippets Groups Projects
Select Git revision
  • 4659d3621c6a52ca95a4d88681d581205e036295
  • main default
  • 35-cgu
  • 34-peertube-support
  • 27-add-autoplay-to-iframe
  • 33-bug-on-youtube-embed-urls
  • RC-Rekall-v1.1-fix_lpo
  • tuleap-140-go-back-to-my-capsules-page-when-i-m-on-capsule-preview-page
  • RC-Rekall-v1.2-fix10
  • RC-Rekall-v1.2-fix9
  • RC-Rekall-v1.2-fix8
  • RC-Rekall-v1.2-fix7
  • RC-Rekall-v1.2-fix6
  • RC-Rekall-v1.2-fix5
  • RC-Rekall-v1.2-fix4
  • RC-Rekall-v1.2-fix3
  • RC-Rekall-v1.2-fix2
  • RC-Rekall-v1.2-fix1
  • RC-Rekall-v1.1-fix-3
  • RC-Rekall-v1.1-fix-2
  • RC-Rekall-v1.1-fix-1
  • RC-Rekall-v1.1-delivered
  • preprod20220209-1535
23 results

CapsuleBuilder.php

Blame
  • CapsuleBuilder.php 4.16 KiB
    <?php
    
    namespace App\Builder;
    
    use App\Entity\Capsule;
    use App\Entity\Group;
    use App\Entity\User;
    use App\Helper\ContractHelper;
    
    class CapsuleBuilder
    {
        public Capsule $capsule;
        private bool $hasRequiredName = false;
        private bool $hasRequiredCreationAuthor = false;
        private bool $hastRequiredCreationDate = false;
        private bool $hasRequiredPreviewLink = false;
        private bool $hasRequiredEditionLink = false;
        private bool $hasRequiredUpdateDate = false;
        private bool $hasPasswordSet = false;
    
        public function __construct()
        {
            $this->capsule = new Capsule();
        }
    
        public function withName(string $name): CapsuleBuilder
        {
            $this->capsule->setName($name);
            $this->hasRequiredName = true;
            return $this;
        }
    
        public function withCreationAuthor(User $creation_author): CapsuleBuilder
        {
            $this->capsule->setCreationAuthor($creation_author);
            $this->hasRequiredCreationAuthor = true;
            $this->capsule->addEditor($creation_author);
            return $this;
        }
    
        public function withCreationDate(\DateTime $creation_date): CapsuleBuilder
        {
            $this->capsule->setCreationDate($creation_date);
            $this->hastRequiredCreationDate = true;
            return $this;
        }
    
    
        public function withLinkPath(string $preview_link): CapsuleBuilder
        {
            $this->capsule->setLinkPath($preview_link);
            $this->hasRequiredPreviewLink = true;
            return $this;
        }
    
        private function createEditionLink(): void
        {
            ContractHelper::requires(
                $this->hasRequiredPreviewLink,
                "The call of CapsuleBuilder::withPreviewLink should be " .
                "called before CapsuleBuilder::createEditionLink"
            );
            ContractHelper::requires(
                $this->hasPasswordSet,
                "The call of CapsuleBuilder::withPassword should be " .
                "called before CapsuleBuilder::createEditionLink"
            );
            $this->capsule->setEditionLink($this->capsule->getLinkPath() .
                "/?p=" . $this->capsule->getPassword());
            $this->hasRequiredEditionLink = true;
        }
    
        public function withUpdateAuthor(User $update_author): CapsuleBuilder
        {
            $this->capsule->setUpdateAuthor($update_author);
            return $this;
        }
    
        public function withUpdateDate(\DateTime $update_date): CapsuleBuilder
        {
            $this->capsule->setUpdatedDate($update_date);
            $this->hasRequiredUpdateDate = true;
            return $this;
        }
    
        public function withPassword(string $password): CapsuleBuilder
        {
            $this->capsule->setPassword($password);
            $this->hasPasswordSet = true;
    
            return $this;
        }
    
        public function withGroup(Group $group): CapsuleBuilder
        {
            $this->capsule->addGroup($group);
            return $this;
        }
    
        public function createCapsule(): Capsule
        {
            $this->createEditionLink();
            ContractHelper::requires(
                $this->hasRequiredName,
                "The call of CapsuleBuilder::withName should be called before CapsuleBuilder::create"
            );
            ContractHelper::requires(
                $this->hasRequiredCreationAuthor,
                "The call of CapsuleBuilder::withCreationAuthor should be called before CapsuleBuilder::create"
            );
            ContractHelper::requires(
                $this->hastRequiredCreationDate,
                "The call of CapsuleBuilder::withCreationDate should be called before CapsuleBuilder::create"
            );
            ContractHelper::requires(
                $this->hasRequiredPreviewLink,
                "The call of CapsuleBuilder::withPreviewLink should be called before CapsuleBuilder::create"
            );
            ContractHelper::requires(
                $this->hasRequiredEditionLink,
                "The call of CapsuleBuilder::withEditionLink should be called before CapsuleBuilder::create"
            );
            ContractHelper::requires(
                $this->hasRequiredUpdateDate,
                "The call of CapsuleBuilder::withUpdateDate should be called before CapsuleBuilder::create"
            );
            ContractHelper::requires(
                $this->hasPasswordSet,
                "The capsule should have its password defined"
            );
    
            return $this->capsule;
        }
    }