1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Glue\CompaniesRestApi\Processor\Company\Reader; |
||
9 | |||
10 | use Generated\Shared\Transfer\CompanyTransfer; |
||
11 | use Generated\Shared\Transfer\RestCompanyAttributesTransfer; |
||
12 | use Spryker\Glue\CompaniesRestApi\CompaniesRestApiConfig; |
||
13 | use Spryker\Glue\CompaniesRestApi\Dependency\Client\CompaniesRestApiToCompanyClientInterface; |
||
14 | use Spryker\Glue\CompaniesRestApi\Processor\Company\Mapper\CompanyMapperInterface; |
||
15 | use Spryker\Glue\CompaniesRestApi\Processor\Company\RestResponseBuilder\CompanyRestResponseBuilderInterface; |
||
16 | use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface; |
||
17 | use Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface; |
||
18 | |||
19 | class CompanyReader implements CompanyReaderInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var \Spryker\Glue\CompaniesRestApi\Dependency\Client\CompaniesRestApiToCompanyClientInterface |
||
23 | */ |
||
24 | protected $companyClient; |
||
25 | |||
26 | /** |
||
27 | * @var \Spryker\Glue\CompaniesRestApi\Processor\Company\Mapper\CompanyMapperInterface |
||
28 | */ |
||
29 | protected $companyMapperInterface; |
||
30 | |||
31 | /** |
||
32 | * @var \Spryker\Glue\CompaniesRestApi\Processor\Company\RestResponseBuilder\CompanyRestResponseBuilderInterface |
||
33 | */ |
||
34 | protected $companyRestResponseBuilder; |
||
35 | |||
36 | /** |
||
37 | * @param \Spryker\Glue\CompaniesRestApi\Dependency\Client\CompaniesRestApiToCompanyClientInterface $companyClient |
||
38 | * @param \Spryker\Glue\CompaniesRestApi\Processor\Company\Mapper\CompanyMapperInterface $companyMapperInterface |
||
39 | * @param \Spryker\Glue\CompaniesRestApi\Processor\Company\RestResponseBuilder\CompanyRestResponseBuilderInterface $companyRestResponseBuilder |
||
40 | */ |
||
41 | public function __construct( |
||
42 | CompaniesRestApiToCompanyClientInterface $companyClient, |
||
43 | CompanyMapperInterface $companyMapperInterface, |
||
44 | CompanyRestResponseBuilderInterface $companyRestResponseBuilder |
||
45 | ) { |
||
46 | $this->companyClient = $companyClient; |
||
47 | $this->companyMapperInterface = $companyMapperInterface; |
||
48 | $this->companyRestResponseBuilder = $companyRestResponseBuilder; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest |
||
53 | * |
||
54 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface |
||
55 | */ |
||
56 | public function getCurrentUserCompany(RestRequestInterface $restRequest): RestResponseInterface |
||
57 | { |
||
58 | if ($this->isResourceIdentifierCurrentUser($restRequest->getResource()->getId())) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
59 | return $this->getCurrentUserCompanies($restRequest); |
||
60 | } |
||
61 | |||
62 | return $this->getCurrentUserCompanyByUuid($restRequest); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest |
||
67 | * |
||
68 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface |
||
69 | */ |
||
70 | protected function getCurrentUserCompanies(RestRequestInterface $restRequest): RestResponseInterface |
||
71 | { |
||
72 | if (!$restRequest->getRestUser()->getIdCompany()) { |
||
73 | return $this->companyRestResponseBuilder->createCompanyUserNotSelectedError(); |
||
74 | } |
||
75 | |||
76 | $companyTransfer = $this->companyClient->getCompanyById( |
||
77 | (new CompanyTransfer())->setIdCompany($restRequest->getRestUser()->getIdCompany()), |
||
78 | ); |
||
79 | |||
80 | if (!$companyTransfer->getUuid()) { |
||
81 | return $this->companyRestResponseBuilder->createCompanyNotFoundError(); |
||
82 | } |
||
83 | |||
84 | return $this->createResponse($companyTransfer); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest |
||
89 | * |
||
90 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface |
||
91 | */ |
||
92 | protected function getCurrentUserCompanyByUuid(RestRequestInterface $restRequest): RestResponseInterface |
||
93 | { |
||
94 | $companyResponseTransfer = $this->companyClient->findCompanyByUuid( |
||
95 | (new CompanyTransfer())->setUuid($restRequest->getResource()->getId()), |
||
96 | ); |
||
97 | |||
98 | if ( |
||
99 | !$companyResponseTransfer->getIsSuccessful() |
||
100 | || !$this->isCurrentCompanyUserInCompany($restRequest, $companyResponseTransfer->getCompanyTransfer()) |
||
101 | ) { |
||
102 | return $this->companyRestResponseBuilder->createCompanyNotFoundError(); |
||
103 | } |
||
104 | |||
105 | return $this->createResponse($companyResponseTransfer->getCompanyTransfer()); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param string $resourceIdentifier |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | protected function isResourceIdentifierCurrentUser(string $resourceIdentifier): bool |
||
114 | { |
||
115 | return $resourceIdentifier === CompaniesRestApiConfig::COLLECTION_IDENTIFIER_CURRENT_USER; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param \Generated\Shared\Transfer\CompanyTransfer $companyTransfer |
||
120 | * |
||
121 | * @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface |
||
122 | */ |
||
123 | protected function createResponse(CompanyTransfer $companyTransfer): RestResponseInterface |
||
124 | { |
||
125 | $restCompanyAttributesTransfer = $this->companyMapperInterface |
||
126 | ->mapCompanyTransferToRestCompanyAttributesTransfer( |
||
127 | $companyTransfer, |
||
128 | new RestCompanyAttributesTransfer(), |
||
129 | ); |
||
130 | |||
131 | return $this->companyRestResponseBuilder |
||
132 | ->createCompanyRestResponse( |
||
133 | $companyTransfer->getUuid(), |
||
134 | $restCompanyAttributesTransfer, |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param \Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface $restRequest |
||
140 | * @param \Generated\Shared\Transfer\CompanyTransfer $companyTransfer |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | protected function isCurrentCompanyUserInCompany( |
||
145 | RestRequestInterface $restRequest, |
||
146 | CompanyTransfer $companyTransfer |
||
147 | ): bool { |
||
148 | return $restRequest->getRestUser() |
||
149 | && $restRequest->getRestUser()->getIdCompany() |
||
150 | && $restRequest->getRestUser()->getIdCompany() === $companyTransfer->getIdCompany(); |
||
151 | } |
||
152 | } |
||
153 |