1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace AppBuilder\Application\Module\ExistingTicketsIndex; |
6
|
|
|
|
7
|
|
|
use AppBuilder\Application\Configuration\ValueObject\Parameters; |
8
|
|
|
use AppBuilder\Event\Application\JiraTicketMappedEvent; |
9
|
|
|
use AppBuilder\Event\Application\JiraTicketMappedEventAware; |
10
|
|
|
use AppBuilder\Event\Application\TicketDirIndexedEvent; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
class ExistingTicketsIndexService implements JiraTicketMappedEventAware |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
private $ticketKey; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $homeDir; |
21
|
|
|
|
22
|
|
|
/** @var ?string */ |
23
|
|
|
private $ticketDir; |
24
|
|
|
|
25
|
|
|
/** @var bool */ |
26
|
|
|
private $ticketExists; |
27
|
|
|
|
28
|
|
|
/** @var EventDispatcherInterface */ |
29
|
|
|
private $dispatcher; |
30
|
|
|
|
31
|
|
|
/** @var LoggerInterface */ |
32
|
|
|
private $logger; |
33
|
|
|
|
34
|
|
|
/** @var int */ |
35
|
|
|
private $id; |
36
|
|
|
|
37
|
|
|
/** @var Parameters */ |
38
|
|
|
private $applicationParams; |
39
|
|
|
|
40
|
2 |
|
public function __construct( |
41
|
|
|
Parameters $applicationParams, |
42
|
|
|
EventDispatcherInterface $dispatcher, |
43
|
|
|
LoggerInterface $logger |
44
|
|
|
) { |
45
|
2 |
|
$this->applicationParams = $applicationParams; |
46
|
2 |
|
$this->homeDir = $applicationParams->projectsHomeDir(); |
47
|
2 |
|
$this->dispatcher = $dispatcher; |
48
|
2 |
|
$this->logger = $logger; |
49
|
2 |
|
} |
50
|
|
|
|
51
|
2 |
|
public function onJiraTicketMapped(JiraTicketMappedEvent $event) : void |
52
|
|
|
{ |
53
|
2 |
|
$this->ticketKey = $event->ticket()['ticket_key']; |
54
|
2 |
|
$this->id = $event->ticket()['id']; |
55
|
2 |
|
$this->searchForTicketDir(); |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Looks for application directory in home directory. |
60
|
|
|
* Returns true and path if found or false and empty string if not. |
61
|
|
|
*/ |
62
|
2 |
|
private function searchForTicketDir() : void |
63
|
|
|
{ |
64
|
2 |
|
$folders = scandir($this->homeDir); |
65
|
2 |
|
if (in_array($this->ticketKey, $folders, false)) { |
66
|
1 |
|
$this->ticketDir = $this->applicationParams->path($this->ticketKey); |
67
|
1 |
|
$this->ticketExists = true; |
68
|
|
|
} else { |
69
|
1 |
|
$this->ticketDir = null; |
70
|
1 |
|
$this->ticketExists = false; |
71
|
|
|
} |
72
|
2 |
|
$this->dispatcher->dispatch( |
73
|
2 |
|
TicketDirIndexedEvent::NAME, |
74
|
2 |
|
new TicketDirIndexedEvent([ |
75
|
2 |
|
'ticketId' => $this->id, |
76
|
2 |
|
'ticketDir' => $this->ticketDir, |
77
|
2 |
|
'ticketExists' => $this->ticketExists, |
78
|
|
|
]) |
79
|
|
|
); |
80
|
2 |
|
} |
81
|
|
|
} |
82
|
|
|
|