|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scrumban\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Scrumban\Registry\RegistryInterface; |
|
6
|
|
|
use Scrumban\Gateway\GatewayInterface; |
|
7
|
|
|
|
|
8
|
|
|
use Scrumban\Utils\CardHelper; |
|
9
|
|
|
|
|
10
|
|
|
use Scrumban\Utils\PlusForTrelloHelper; |
|
11
|
|
|
|
|
12
|
|
|
use Scrumban\Entity\Sprint; |
|
13
|
|
|
|
|
14
|
|
|
class BoardManager |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var RegistryInterface **/ |
|
17
|
|
|
protected $registry; |
|
18
|
|
|
/** @var GatewayInterface **/ |
|
19
|
|
|
protected $gateway; |
|
20
|
|
|
/** @var UserStoryManager **/ |
|
21
|
|
|
protected $userStoryManager; |
|
22
|
|
|
/** @var SprintManager **/ |
|
23
|
|
|
protected $sprintManager; |
|
24
|
|
|
/** @var bool **/ |
|
25
|
|
|
public $hasPlusForTrello; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(RegistryInterface $registry, GatewayInterface $gateway, UserStoryManager $userStoryManager, SprintManager $sprintManager) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->registry = $registry; |
|
30
|
|
|
$this->gateway = $gateway; |
|
31
|
|
|
$this->userStoryManager = $userStoryManager; |
|
32
|
|
|
$this->sprintManager = $sprintManager; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function sync($boardName) |
|
36
|
|
|
{ |
|
37
|
|
|
if (($boardData = $this->registry->getBoard($boardName)) === null) { |
|
38
|
|
|
throw new \InvalidArgumentException('No board is configured for the given name'); |
|
39
|
|
|
} |
|
40
|
|
|
$this->init(); |
|
41
|
|
|
$columns = $this->gateway->getBoardColumns($boardData['id']); |
|
|
|
|
|
|
42
|
|
|
$currentSprint = $this->sprintManager->getCurrentSprint(); |
|
43
|
|
|
|
|
44
|
|
|
foreach ($columns as $columnData) { |
|
45
|
|
|
yield "comment" => "Scanning column {$columnData['name']}..."; |
|
46
|
|
|
if (($column = $this->registry->getColumn(CardHelper::slugify($columnData['name']))) === null) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
$messages = $this->processColumnCards($columnData['id'], $column, $currentSprint); |
|
50
|
|
|
foreach($messages as $type => $message) { |
|
51
|
|
|
yield $type => $message; |
|
52
|
|
|
} |
|
53
|
|
|
yield "info" => "Column {$columnData['name']} has been synchronized"; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function init() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->registry->storeUserStories($this->userStoryManager->getAll()); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function processColumnCards(string $columnId, array $column, Sprint $sprint = null) |
|
63
|
|
|
{ |
|
64
|
|
|
$cards = $this->gateway->getColumnCards($columnId); |
|
|
|
|
|
|
65
|
|
|
yield "info" => count($cards) . " cards to synchronize with status {$column['status']}"; |
|
66
|
|
|
foreach ($cards as $card) { |
|
67
|
|
|
$titleParts = explode('|', $card['name']); |
|
68
|
|
|
$extraData = isset($titleParts[1]) ? trim($titleParts[1]): ''; |
|
69
|
|
|
|
|
70
|
|
|
$estimations = $this->getCardEstimations($card, $extraData); |
|
71
|
|
|
|
|
72
|
|
|
$method = |
|
73
|
|
|
(($userStory = $this->registry->getUserStory($card['id'])) === null) |
|
|
|
|
|
|
74
|
|
|
? 'createUserStory' |
|
75
|
|
|
: 'updateUserStory' |
|
76
|
|
|
; |
|
77
|
|
|
$this->userStoryManager->{$method}( |
|
78
|
|
|
$card['id'], |
|
79
|
|
|
trim($titleParts[0]), |
|
80
|
|
|
$card['desc'], |
|
81
|
|
|
(!empty($extraData)) ? CardHelper::extractValue($extraData) : 0, |
|
82
|
|
|
$column['status'], |
|
83
|
|
|
$estimations['estimated'], |
|
84
|
|
|
$estimations['spent'], |
|
85
|
|
|
CardHelper::isInCurrentSprint($column['status']) ? $sprint : null, |
|
86
|
|
|
$userStory |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function getCardEstimations(array $card, string $extraData): array |
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->hasPlusForTrello !== true || empty($extraData)) { |
|
94
|
|
|
return [ |
|
95
|
|
|
'estimated' => CardHelper::extractEstimatedTime($extraData), |
|
96
|
|
|
'spent' => CardHelper::extractSpentTime($extraData) |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
if ($card['badges']['comments'] === 0) { |
|
100
|
|
|
return ['estimated' => 0, 'spent' => 0]; |
|
101
|
|
|
} |
|
102
|
|
|
return PlusForTrelloHelper::extractEstimations($this->gateway->getCardComments($card['id'])); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |