1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace AppBuilder\Application\Module\TaskManager\Factory; |
6
|
|
|
|
7
|
|
|
use AppBuilder\Application\Configuration\ValueObject\Parameters; |
8
|
|
|
use AppBuilder\Application\Model\ValueObject\Ticket; |
9
|
|
|
use AppBuilder\Application\Module\Jira\ValueObject\JiraTicketStatus; |
10
|
|
|
use AppBuilder\Application\Module\TaskManager\Exception\MisguidedTaskException; |
11
|
|
|
use AppBuilder\Application\Module\TaskManager\Task\UpdateTicketTask; |
12
|
|
|
use AppBuilder\Application\Utils\FileManager\FileManagerService; |
13
|
|
|
use FileNotFoundException; |
14
|
|
|
|
15
|
|
|
class UpdateTicketTaskFactory |
16
|
|
|
{ |
17
|
|
|
/** @var int */ |
18
|
|
|
public const PRIORITY = 20; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private const COMPARED_STATUS = JiraTicketStatus::WORK_FINISHED; |
22
|
|
|
|
23
|
|
|
/** @var Parameters */ |
24
|
|
|
private $applicationParams; |
25
|
|
|
|
26
|
|
|
/** @var FileManagerService */ |
27
|
|
|
private $fileManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* If application is up to date throws MisguidedTaskException. |
31
|
|
|
* Otherwise returns UpdateTicketTask. |
32
|
|
|
* |
33
|
|
|
* @throws FileNotFoundException |
34
|
|
|
* @throws MisguidedTaskException |
35
|
|
|
*/ |
36
|
5 |
|
public function create( |
37
|
|
|
Ticket $ticket, |
38
|
|
|
Parameters $applicationParams, |
39
|
|
|
FileManagerService $fileManager |
40
|
|
|
) : UpdateTicketTask { |
41
|
5 |
|
$this->applicationParams = $applicationParams; |
42
|
5 |
|
$this->fileManager = $fileManager; |
43
|
5 |
|
if ($this->isUpToDate($ticket)) { |
44
|
3 |
|
throw new MisguidedTaskException(UpdateTicketTask::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
return new UpdateTicketTask($ticket, $applicationParams, $this->fileManager); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Checks wether ticket status is same to in-application status |
52
|
|
|
* |
53
|
|
|
* @throws FileNotFoundException |
54
|
|
|
*/ |
55
|
2 |
|
protected function isWorkFinished(Ticket $ticket, string $filepath) : bool |
56
|
|
|
{ |
57
|
2 |
|
if (!$this->fileManager->fileExists($filepath)) { |
58
|
1 |
|
throw new FileNotFoundException('Could not open ' . $this->applicationParams->snapshotFileName()); |
59
|
|
|
} |
60
|
1 |
|
$content = $this->fileManager->fileGetContent($filepath); |
61
|
|
|
|
62
|
1 |
|
$tempArray = explode(';', $content); |
63
|
|
|
|
64
|
|
|
$snapshot = [ |
65
|
1 |
|
'status' => $tempArray[0], |
66
|
1 |
|
'timestamp' => $tempArray[1], |
67
|
|
|
]; |
68
|
|
|
|
69
|
1 |
|
return $ticket->compareStatus($snapshot['status']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Checks whether application in directory is up to date. |
74
|
|
|
* Returns false if status changes to 'work finished'. |
75
|
|
|
* |
76
|
|
|
* @throws FileNotFoundException |
77
|
|
|
*/ |
78
|
5 |
|
private function isUpToDate(Ticket $ticket) : bool |
79
|
|
|
{ |
80
|
5 |
|
if (!$ticket->compareStatus(static::COMPARED_STATUS)) { |
81
|
2 |
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** @var string */ |
85
|
3 |
|
$filepath = $this->fileManager |
86
|
3 |
|
->combineFilepath( |
87
|
3 |
|
$this->applicationParams->projectsHomeDir(), |
88
|
3 |
|
$ticket->key(), |
89
|
3 |
|
$this->applicationParams->snapshotFileName() |
90
|
|
|
); |
91
|
|
|
|
92
|
3 |
|
if ($ticket->hasDirectory()) { |
93
|
2 |
|
return !$this->isWorkFinished($ticket, $filepath); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|