MinistryOfTruth   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 32
c 1
b 0
f 0
dl 0
loc 125
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 4 1
A analyseByLocale() 0 28 1
A __construct() 0 14 1
A analyse() 0 5 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\Analyser;
17
18
use DateTime;
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\Index\RequestDto as IndexRequest;
23
use SymfonyDoge\MinistryOfTruthClient\Enum\Request\Index\Context;
24
use Veslo\AnthillBundle\Entity\Vacancy;
25
use Veslo\AppBundle\Integration\MinistryOfTruth\LocaleOptionsTrait;
26
use Veslo\SanityBundle\Dto\Vacancy\IndexDto;
27
use Veslo\SanityBundle\Vacancy\Analyser\DataConverter\MinistryOfTruth as DataConverter;
28
use Veslo\SanityBundle\Vacancy\AnalyserInterface;
29
30
/**
31
 * Provides vacancy index, tags and other sanity data for a vacancy by external Ministry of Truth microservice
32
 */
33
class MinistryOfTruth implements AnalyserInterface
34
{
35
    use LocaleOptionsTrait {
36
        configureLocaleOptions as protected;
37
    }
38
39
    /**
40
     * The Ministry of Truth API client
41
     *
42
     * @var ClientInterface
43
     */
44
    private $motClient;
45
46
    /**
47
     * Holds context of security parameters for building requests to the Ministry of Truth API endpoint
48
     *
49
     * @var CredentialsStorageInterface
50
     */
51
    private $credentialsStorage;
52
53
    /**
54
     * Converts data from external format to local dto
55
     *
56
     * @var DataConverter
57
     */
58
    private $dataConverter;
59
60
    /**
61
     * Options for the vacancy analyser
62
     *
63
     * Example:
64
     * ```
65
     * [
66
     *     'default_locale' => 'ru',
67
     *     'locales' => ['ru', 'ua', 'en']
68
     * ]
69
     * ```
70
     *
71
     * @var array
72
     */
73
    private $options;
74
75
    /**
76
     * MinistryOfTruth constructor.
77
     *
78
     * @param ClientInterface             $motClient          The Ministry of Truth API client
79
     * @param CredentialsStorageInterface $credentialsStorage Context of security parameters for building requests
80
     * @param DataConverter               $dataConverter      Converts data from external format to local dto
81
     * @param array                       $options            Options for the vacancy analyser
82
     */
83
    public function __construct(
84
        ClientInterface $motClient,
85
        CredentialsStorageInterface $credentialsStorage,
86
        DataConverter $dataConverter,
87
        array $options
88
    ) {
89
        $this->motClient          = $motClient;
90
        $this->credentialsStorage = $credentialsStorage;
91
        $this->dataConverter      = $dataConverter;
92
93
        $optionsResolver = new OptionsResolver();
94
        $this->configureOptions($optionsResolver);
95
96
        $this->options = $optionsResolver->resolve($options);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function analyse(Vacancy $vacancy): IndexDto
103
    {
104
        $localeDefault = $this->options['default_locale'];
105
106
        return $this->analyseByLocale($vacancy, $localeDefault);
107
    }
108
109
    /**
110
     * Returns a sanity index data for the vacancy with specified locale
111
     *
112
     * @param Vacancy $vacancy Vacancy entity
113
     * @param string  $locale  Locale for sanity data translation
114
     *
115
     * @return IndexDto
116
     */
117
    public function analyseByLocale(Vacancy $vacancy, string $locale): IndexDto
118
    {
119
        $indexRequest = new IndexRequest();
120
        $indexRequest->setLocale($locale);
121
122
        $authorizationToken = $this->credentialsStorage->getAuthorizationToken();
123
        $indexRequest->setAuthorizationToken($authorizationToken);
124
125
        $vacancyDescription = $vacancy->getText();
126
127
        // TODO: extract into the purifier service.
128
        $vacancyDescription = strip_tags($vacancyDescription);
129
        $vacancyDescription = str_replace([':', ';', '.', ',', '-', '/', '?', '!'], ' ', $vacancyDescription);
130
131
        $indexRequest->addContext(Context\Vacancy::DESCRIPTION, $vacancyDescription);
132
133
        $indexResponse = $this->motClient->index($indexRequest);
134
        $vacancyIndex  = $indexResponse->getIndex();
135
136
        $indexDto = $this->dataConverter->convertIndex($vacancyIndex);
137
138
        $vacancyId = $vacancy->getId();
139
        $indexDto->setVacancyId($vacancyId);
140
141
        $indexationDate = new DateTime();
142
        $indexDto->setIndexationDate($indexationDate);
143
144
        return $indexDto;
145
    }
146
147
    /**
148
     * Performs options configuration for the vacancy analyser
149
     *
150
     * @param OptionsResolver $optionsResolver Validates options and merges them with default values
151
     *
152
     * @return void
153
     */
154
    protected function configureOptions(OptionsResolver $optionsResolver): void
155
    {
156
        // 'default_locale', 'locales'
157
        $this->configureLocaleOptions($optionsResolver);
158
    }
159
}
160