From d3fa2170f75aea0decd7016e2289e2249c06d312 Mon Sep 17 00:00:00 2001 From: weichien00 Date: Sat, 26 Jun 2021 00:27:55 +0800 Subject: [PATCH] new: assign segment to company created. #1 --- .../Processors/AssignSegmentProcessor.php | 56 +++++++++++++++ .../Services/AssignsCompanyToSegment.php | 33 +++++++++ .../Standards/Rules/CanAssignSegment.php | 40 +++++++++++ .../DataTransferObjects/ConstantObject.php | 57 +++++++++++++++ .../Segments/Services/FetchesSegment.php | 34 +++++++++ .../ServiceConfigurationsObject.php | 71 +++++++++++++++++++ .../Constants/SegmentConstants.php | 23 ++++++ app/Models/Employee.php | 34 +++++++++ app/Models/Segment.php | 30 ++++++++ 9 files changed, 378 insertions(+) create mode 100644 app/Classes/Modules/Companies/Processors/AssignSegmentProcessor.php create mode 100644 app/Classes/Modules/Companies/Services/AssignsCompanyToSegment.php create mode 100644 app/Classes/Modules/Companies/Standards/Rules/CanAssignSegment.php create mode 100644 app/Classes/Modules/Segments/DataTransferObjects/ConstantObject.php create mode 100644 app/Classes/Modules/Segments/Services/FetchesSegment.php create mode 100644 app/Classes/Modules/ServiceTypes/DataTransferObjects/ServiceConfigurationsObject.php create mode 100644 app/Classes/ValueObjects/Constants/SegmentConstants.php create mode 100644 app/Models/Employee.php create mode 100644 app/Models/Segment.php diff --git a/app/Classes/Modules/Companies/Processors/AssignSegmentProcessor.php b/app/Classes/Modules/Companies/Processors/AssignSegmentProcessor.php new file mode 100644 index 00000000..66cfb08e --- /dev/null +++ b/app/Classes/Modules/Companies/Processors/AssignSegmentProcessor.php @@ -0,0 +1,56 @@ +canAssignSegment = $canAssignSegment; + $this->assignsSegment = $assignsSegment; + $this->fetchesSegment = $fetchesSegment; + } + + + /** + * @param Company $company + * @param int|null $segmentId + * @return Model + * @throws \App\Classes\Exceptions\AccessForbiddenException + * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\RequestValidationException + */ + public function execute(Company $company, ?int $segmentId = SegmentConstants::STANDARD_SEGMENT): Model { + + $segment = $this->fetchesSegment->execute(['id' => $segmentId]); + + $this->canAssignSegment->passes(); + + return $this->assignsSegment->execute($company, $segment); + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Companies/Services/AssignsCompanyToSegment.php b/app/Classes/Modules/Companies/Services/AssignsCompanyToSegment.php new file mode 100644 index 00000000..81b6e964 --- /dev/null +++ b/app/Classes/Modules/Companies/Services/AssignsCompanyToSegment.php @@ -0,0 +1,33 @@ +segments()->attach($segment); + + return $company; + + } catch (QueryException $exception){ + throw new MalformedRequestException($exception); + } + + + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Companies/Standards/Rules/CanAssignSegment.php b/app/Classes/Modules/Companies/Standards/Rules/CanAssignSegment.php new file mode 100644 index 00000000..36fd2a41 --- /dev/null +++ b/app/Classes/Modules/Companies/Standards/Rules/CanAssignSegment.php @@ -0,0 +1,40 @@ +name = $name; + $this->reference = $reference; + $this->detail = $detail; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return string + */ + public function getReference(): string + { + return $this->reference; + } + + /** + * @return array + */ + public function getDetail(): array + { + return $this->detail; + } + + +} \ No newline at end of file diff --git a/app/Classes/Modules/Segments/Services/FetchesSegment.php b/app/Classes/Modules/Segments/Services/FetchesSegment.php new file mode 100644 index 00000000..f76580f9 --- /dev/null +++ b/app/Classes/Modules/Segments/Services/FetchesSegment.php @@ -0,0 +1,34 @@ +repository = $repository; + } + + + /** + * @return Builder + */ + public function getRepository(): Builder + { + return $this->repository->newQuery(); + } +} \ No newline at end of file diff --git a/app/Classes/Modules/ServiceTypes/DataTransferObjects/ServiceConfigurationsObject.php b/app/Classes/Modules/ServiceTypes/DataTransferObjects/ServiceConfigurationsObject.php new file mode 100644 index 00000000..95b5fd52 --- /dev/null +++ b/app/Classes/Modules/ServiceTypes/DataTransferObjects/ServiceConfigurationsObject.php @@ -0,0 +1,71 @@ +service = $service; + $this->configurations = $configurations; + $this->customOptions = $customOptions; + } + + /** + * @return ServiceType + */ + public function getService(): ServiceType + { + return $this->service; + } + + + + /** + * @return SegmentConstant + */ + public function getConfigurations(): SegmentConstant + { + return $this->configurations; + } + + /** + * @return Collection + */ + public function getCustomOptions(): Collection + { + return $this->customOptions->map(function($value){ + return new CustomConfigurationsObject($this->getConfigurations(), $value); + }); + } + + public function getBaseCurrencyRates(){ + return (App()->make(FetchesServiceCurrenciesConfigurations::class))->execute($this->configurations, SegmentConstants::SERVICE_TYPE); + } + + +} \ No newline at end of file diff --git a/app/Classes/ValueObjects/Constants/SegmentConstants.php b/app/Classes/ValueObjects/Constants/SegmentConstants.php new file mode 100644 index 00000000..80e9cb71 --- /dev/null +++ b/app/Classes/ValueObjects/Constants/SegmentConstants.php @@ -0,0 +1,23 @@ +HasMany(Company::class, 'company_id', 'id'); + } + + /** + * @return \Illuminate\Database\Eloquent\Relations\HasOne + **/ + public function user(): HasOne + { + return $this->hasOne(User::class, 'user_id', 'id'); + } +} diff --git a/app/Models/Segment.php b/app/Models/Segment.php new file mode 100644 index 00000000..339e8e86 --- /dev/null +++ b/app/Models/Segment.php @@ -0,0 +1,30 @@ +HasMany(SegmentConstant::class, 'segment_id', 'id'); + } +}