-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPersonioApiService.php
143 lines (122 loc) · 4.63 KB
/
PersonioApiService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS extension "personio_jobs".
*
* Copyright (C) 2023 Elias Häußler <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace CPSIT\Typo3PersonioJobs\Service;
use CPSIT\Typo3PersonioJobs\Configuration\ExtensionConfiguration;
use CPSIT\Typo3PersonioJobs\Domain\Model\Job;
use CPSIT\Typo3PersonioJobs\Domain\Model\JobDescription;
use CPSIT\Typo3PersonioJobs\Event\AfterJobsMappedEvent;
use CPSIT\Typo3PersonioJobs\Exception\MalformedApiResponseException;
use CPSIT\Typo3PersonioJobs\Utility\FrontendUtility;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Tree\Message\Messages;
use CuyZ\Valinor\Mapper\TreeMapper;
use CuyZ\Valinor\MapperBuilder;
use EliasHaeussler\ValinorXml\Exception\ArrayPathHasUnexpectedType;
use EliasHaeussler\ValinorXml\Exception\ArrayPathIsInvalid;
use EliasHaeussler\ValinorXml\Exception\XmlIsMalformed;
use EliasHaeussler\ValinorXml\Mapper\Source\XmlSource;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Localization\Locale;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
/**
* PersonioApiService
*
* @author Elias Häußler <[email protected]>
* @license GPL-2.0-or-later
*/
final class PersonioApiService
{
private readonly Uri $apiUrl;
private readonly TreeMapper $mapper;
public function __construct(
private readonly RequestFactory $requestFactory,
private readonly EventDispatcherInterface $eventDispatcher,
ExtensionConfiguration $extensionConfiguration,
) {
$this->apiUrl = $extensionConfiguration->getApiUrl();
$this->mapper = $this->createMapper();
}
/**
* @return list<Job>
* @throws ArrayPathHasUnexpectedType
* @throws ArrayPathIsInvalid
* @throws XmlIsMalformed
*/
public function getJobs(): array
{
$requestUri = $this->apiUrl->withPath('/xml');
$response = $this->requestFactory->request((string)$requestUri);
$source = XmlSource::fromXmlString((string)$response->getBody())
->asCollection('position')
->asCollection('position.*.jobDescriptions.jobDescription')
;
try {
$jobs = $this->mapper->map('list<' . Job::class . '>', $source['position']);
$this->eventDispatcher->dispatch(new AfterJobsMappedEvent($requestUri, $jobs));
return $jobs;
} catch (MappingError $error) {
$errors = Messages::flattenFromNode($error->node())->errors();
throw MalformedApiResponseException::forMappingErrors($errors);
}
}
public function getJobUrl(Job $job): Uri
{
$language = $this->getLanguageCode();
$jobUrl = $this->apiUrl->withPath(sprintf('/job/%d', $job->getPersonioId()));
if ($language !== null) {
$jobUrl = $jobUrl->withQuery(sprintf('?language=%s', $language));
}
return $jobUrl;
}
public function getApplyUrl(Job $job): Uri
{
return $this->getJobUrl($job)->withFragment('apply');
}
private function createMapper(): TreeMapper
{
return (new MapperBuilder())
->supportDateFormats(\DateTimeInterface::ATOM)
->allowSuperfluousKeys()
->enableFlexibleCasting()
->registerConstructor(
Job::fromApiResponse(...),
JobDescription::fromApiResponse(...),
)
->mapper()
;
}
private function getLanguageCode(): ?string
{
$serverRequest = FrontendUtility::getServerRequest();
$siteLanguage = $serverRequest->getAttribute('language');
if (!($siteLanguage instanceof SiteLanguage)) {
return null;
}
$locale = $siteLanguage->getLocale();
if ($locale instanceof Locale) {
return $locale->getLanguageCode();
}
// @todo Remove once support for TYPO3 v11 is dropped
return $siteLanguage->getTwoLetterIsoCode();
}
}