MinistryOfTruth   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 6
eloc 38
c 4
b 1
f 0
dl 0
loc 142
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 16 2
A getTagGroups() 0 17 2
A requestTagGroups() 0 11 1
A __construct() 0 16 1
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\SanityBundle\Vacancy\Tag\Group\Provider;
17
18
use Symfony\Component\Cache\Adapter\AdapterInterface;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use SymfonyDoge\MinistryOfTruthClient\Bridge\Symfony\Credentials\StorageInterface as CredentialsStorageInterface;
21
use SymfonyDoge\MinistryOfTruthClient\ClientInterface;
22
use SymfonyDoge\MinistryOfTruthClient\Dto\Request\Tag\Group\Get\All\RequestDto as GetTagGroupsRequest;
23
use SymfonyDoge\MinistryOfTruthClient\Dto\Response\Tag\Group\ContentDto;
24
use Veslo\AppBundle\Integration\MinistryOfTruth\LocaleOptionsTrait;
25
use Veslo\SanityBundle\Vacancy\Tag\Group\Provider\DataConverter\MinistryOfTruth as DataConverter;
26
use Veslo\SanityBundle\Vacancy\Tag\Group\ProviderInterface;
27
28
/**
29
 * Uses the Ministry of Truth API to provide sanity tag groups for vacancy analysers
30
 */
31
class MinistryOfTruth implements ProviderInterface
32
{
33
    use LocaleOptionsTrait {
34
        configureLocaleOptions as protected;
35
    }
36
37
    /**
38
     * The Ministry of Truth API client
39
     *
40
     * @var ClientInterface
41
     */
42
    private $motClient;
43
44
    /**
45
     * Holds context of security parameters for building requests to the Ministry of Truth API endpoint
46
     *
47
     * @var CredentialsStorageInterface
48
     */
49
    private $credentialsStorage;
50
51
    /**
52
     * Converts sanity tag groups data from external format to local data transfer objects
53
     *
54
     * @var DataConverter
55
     */
56
    private $dataConverter;
57
58
    /**
59
     * Holds result of last API call for current process
60
     *
61
     * @var AdapterInterface
62
     */
63
    private $cache;
64
65
    /**
66
     * Options for the sanity tags group provider
67
     *
68
     * Example:
69
     * ```
70
     * [
71
     *     'cache' => [
72
     *         'namespace' => 'veslo.sanity.vacancy.tag.group.provider.ministry_of_truth'
73
     *     ],
74
     *     'default_locale' => 'ru',
75
     *     'locale' => ['ru', 'ua', 'en']
76
     * ]
77
     * ```
78
     *
79
     * @var array
80
     */
81
    private $options;
82
83
    /**
84
     * MinistryOfTruth constructor.
85
     *
86
     * @param ClientInterface             $motClient          The Ministry of Truth API client
87
     * @param CredentialsStorageInterface $credentialsStorage Holds context of security parameters for building requests
88
     * @param DataConverter               $dataConverter      Converts group data from external format to local DTOs
89
     * @param AdapterInterface            $cache              Holds result of last API call for current process
90
     * @param array                       $options            Options for the sanity tags group provider
91
     */
92
    public function __construct(
93
        ClientInterface $motClient,
94
        CredentialsStorageInterface $credentialsStorage,
95
        DataConverter $dataConverter,
96
        AdapterInterface $cache,
97
        array $options
98
    ) {
99
        $this->motClient          = $motClient;
100
        $this->credentialsStorage = $credentialsStorage;
101
        $this->dataConverter      = $dataConverter;
102
        $this->cache              = $cache;
103
104
        $optionsResolver = new OptionsResolver();
105
        $this->configureOptions($optionsResolver);
106
107
        $this->options = $optionsResolver->resolve($options);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getTagGroups(): iterable
114
    {
115
        $cacheKey            = $this->options['cache']['namespace'] . __FUNCTION__;
116
        $groupDtoArrayCached = $this->cache->getItem($cacheKey);
117
118
        if ($groupDtoArrayCached->isHit()) {
119
            return $groupDtoArrayCached->get();
120
        }
121
122
        $tagGroups = $this->requestTagGroups();
123
124
        $groupDtoArray = $this->dataConverter->convertTagGroups($tagGroups);
125
126
        $groupDtoArrayCached->set($groupDtoArray);
127
        $this->cache->save($groupDtoArrayCached);
128
129
        return $groupDtoArray;
130
    }
131
132
    /**
133
     * Returns sanity tag groups in format of integration layer
134
     *
135
     * @return ContentDto[]
136
     */
137
    protected function requestTagGroups(): array
138
    {
139
        $request = new GetTagGroupsRequest();
140
        $request->setLocale($this->options['default_locale']);
141
142
        $authorizationToken = $this->credentialsStorage->getAuthorizationToken();
143
        $request->setAuthorizationToken($authorizationToken);
144
145
        $tagGroupsResponse = $this->motClient->getTagGroups($request);
146
147
        return $tagGroupsResponse->getTagGroups();
148
    }
149
150
    /**
151
     * Performs options configuration for sanity tag groups provider
152
     *
153
     * @param OptionsResolver $optionsResolver Validates options and merges them with default values
154
     *
155
     * @return void
156
     */
157
    protected function configureOptions(OptionsResolver $optionsResolver): void
158
    {
159
        if (method_exists(OptionsResolver::class, 'isNested')) {
160
            $optionsResolver->setDefault(
161
                'cache',
162
                function (OptionsResolver $cacheOptionsResolver) {
163
                    $cacheOptionsResolver->setDefault('namespace', md5(__CLASS__));
164
                }
165
            );
166
        // TODO: [upgrade] remove after migration to ~4.2 (options resolver prior to 4.2 doesn't support nested options)
167
        } else {
168
            $optionsResolver->setDefault('cache', ['namespace' => md5(__CLASS__)]);
169
        }
170
171
        // 'default_locale', 'locales'
172
        $this->configureLocaleOptions($optionsResolver);
173
    }
174
}
175