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\AnthillBundle\Vacancy; |
17
|
|
|
|
18
|
|
|
use DateTime; |
19
|
|
|
use Veslo\AnthillBundle\Company\Creator as CompanyCreator; |
20
|
|
|
use Veslo\AnthillBundle\Dto\Vacancy\Parser\ParsedDto; |
21
|
|
|
use Veslo\AnthillBundle\Dto\Vacancy\RawDto; |
22
|
|
|
use Veslo\AnthillBundle\Entity\Company; |
23
|
|
|
use Veslo\AnthillBundle\Entity\Repository\VacancyRepository; |
24
|
|
|
use Veslo\AnthillBundle\Entity\Vacancy; |
25
|
|
|
use Veslo\AnthillBundle\Entity\Vacancy\Category; |
26
|
|
|
use Veslo\AnthillBundle\Vacancy\Category\Resolver as CategoryResolver; |
27
|
|
|
use Veslo\AppBundle\Entity\Repository\BaseEntityRepository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates and persists a new vacancy instance in local storage |
31
|
|
|
*/ |
32
|
|
|
class Creator |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Creates and persists a new company in local storage |
36
|
|
|
* |
37
|
|
|
* @var CompanyCreator |
38
|
|
|
*/ |
39
|
|
|
private $companyCreator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Extracts a category instance from the specified context |
43
|
|
|
* |
44
|
|
|
* @var CategoryResolver |
45
|
|
|
*/ |
46
|
|
|
private $categoryResolver; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Vacancy repository |
50
|
|
|
* |
51
|
|
|
* @var VacancyRepository |
52
|
|
|
*/ |
53
|
|
|
private $vacancyRepository; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Company repository |
57
|
|
|
* |
58
|
|
|
* @var BaseEntityRepository |
59
|
|
|
*/ |
60
|
|
|
private $companyRepository; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creator constructor. |
64
|
|
|
* |
65
|
|
|
* @param CompanyCreator $companyCreator Creates and persists a new company in local storage |
66
|
|
|
* @param CategoryResolver $categoryResolver Extracts a category instance from the specified context |
67
|
|
|
* @param VacancyRepository $vacancyRepository Vacancy repository |
68
|
|
|
* @param BaseEntityRepository $companyRepository Company repository |
69
|
|
|
*/ |
70
|
|
|
public function __construct( |
71
|
|
|
CompanyCreator $companyCreator, |
72
|
|
|
CategoryResolver $categoryResolver, |
73
|
|
|
VacancyRepository $vacancyRepository, |
74
|
|
|
BaseEntityRepository $companyRepository |
75
|
|
|
) { |
76
|
|
|
$this->companyCreator = $companyCreator; |
77
|
|
|
$this->categoryResolver = $categoryResolver; |
78
|
|
|
$this->vacancyRepository = $vacancyRepository; |
79
|
|
|
$this->companyRepository = $companyRepository; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates and returns a newly vacancy entity by specified parsing context |
84
|
|
|
* |
85
|
|
|
* @param ParsedDto $scanResult Context of parsed vacancy data |
86
|
|
|
* |
87
|
|
|
* @return Vacancy |
88
|
|
|
*/ |
89
|
|
|
public function createByParsedDto(ParsedDto $scanResult): Vacancy |
90
|
|
|
{ |
91
|
|
|
$location = $scanResult->getLocation(); |
92
|
|
|
$roadmap = $location->getRoadmap(); |
93
|
|
|
$data = $scanResult->getVacancy(); |
94
|
|
|
|
95
|
|
|
$vacancy = new Vacancy(); |
96
|
|
|
$vacancy->setRoadmapName($roadmap->getName()); |
97
|
|
|
$vacancy->setExternalIdentifier($data->getExternalIdentifier()); |
98
|
|
|
$vacancy->setSynchronizationDate(new DateTime()); |
99
|
|
|
$vacancy->setUrl($data->getUrl()); |
100
|
|
|
$vacancy->setRegionName($data->getRegionName()); |
101
|
|
|
$vacancy->setTitle($data->getTitle()); |
102
|
|
|
$vacancy->setSnippet($data->getSnippet()); |
103
|
|
|
$vacancy->setText($data->getText()); |
104
|
|
|
$vacancy->setSalaryFrom($data->getSalaryFrom()); |
105
|
|
|
$vacancy->setSalaryTo($data->getSalaryTo()); |
106
|
|
|
$vacancy->setSalaryCurrency($data->getSalaryCurrency()); |
107
|
|
|
$vacancy->setPublicationDate($data->getPublicationDate()); |
108
|
|
|
|
109
|
|
|
$company = $this->resolveCompany($data); |
110
|
|
|
$category = $this->categoryResolver->resolveByRoadmap($roadmap); |
111
|
|
|
|
112
|
|
|
$vacancy->setCompany($company); |
113
|
|
|
|
114
|
|
|
if ($category instanceof Category) { |
115
|
|
|
$vacancy->addCategory($category); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->vacancyRepository->save($vacancy); |
119
|
|
|
|
120
|
|
|
return $vacancy; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns related company entity if exists or calls the company creator to build a new one |
125
|
|
|
* |
126
|
|
|
* @param RawDto $vacancyData Context of raw vacancy data from website |
127
|
|
|
* |
128
|
|
|
* @return Company |
129
|
|
|
*/ |
130
|
|
|
private function resolveCompany(RawDto $vacancyData): Company |
131
|
|
|
{ |
132
|
|
|
$companyName = $vacancyData->getCompanyName(); |
133
|
|
|
$company = $this->companyRepository->findOneByName($companyName); |
|
|
|
|
134
|
|
|
|
135
|
|
|
if (!$company instanceof Company) { |
136
|
|
|
$company = $this->companyCreator->createByVacancyRawDto($vacancyData, true); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $company; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|