getExecutedDefinitionClassNames()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Deployee\Plugins\DeployHistory\Subscriber;
4
5
use Deployee\Components\DocBlock\DocBlock;
6
use Deployee\Components\Persistence\LazyPDO;
7
use Deployee\Plugins\Deploy\Events\FindExecutableDefinitionFilesEvent;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
class FindExecutableDefinitionsSubscriber implements EventSubscriberInterface
12
{
13
    /**
14
     * @var LazyPDO
15
     */
16
    private $lazyPdo;
17
18
    /**
19
     * @var DocBlock
20
     */
21
    private $docBlock;
22
23
    /**
24
     * @param LazyPDO $lazyPdo
25
     * @param DocBlock $docBlock
26
     */
27
    public function __construct(LazyPDO $lazyPdo, DocBlock $docBlock)
28
    {
29
        $this->lazyPdo = $lazyPdo;
30
        $this->docBlock = $docBlock;
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public static function getSubscribedEvents(): array
37
    {
38
        return [
39
            FindExecutableDefinitionFilesEvent::class => 'onFindExecutableDefinitions'
40
        ];
41
    }
42
43
    /**
44
     * @param FindExecutableDefinitionFilesEvent $event
45
     * @throws \ReflectionException
46
     */
47
    public function onFindExecutableDefinitions(FindExecutableDefinitionFilesEvent $event)
48
    {
49
        $collection = $event->getDefinitionFileCollection();
50
        $executedClassNames = $this->getExecutedDefinitionClassNames();
51
52
        foreach($collection->getArrayCopy() as $index => $class){
53
            if($this->docBlock->hasTag($class, 'runalways')
54
                || !in_array($class, $executedClassNames, false)){
55
                continue;
56
            }
57
58
            $event->getOutput()->writeln(
59
                sprintf('Deployment %s already executed. Skipping', $class),
60
                OutputInterface::VERBOSITY_DEBUG
61
            );
62
63
            $collection->offsetUnset($index);
64
        }
65
    }
66
67
    /**
68
     * @return string[]
69
     */
70
    private function getExecutedDefinitionClassNames(): array
71
    {
72
        $return = [];
73
        $sql = 'SELECT name FROM deployee_exec_history WHERE success = 1';
74
        $results = $this->lazyPdo->query($sql, \PDO::FETCH_ASSOC);
75
76
        foreach($results as $row){
77
            $return[] = $row['name'];
78
        }
79
80
        return $return;
81
    }
82
}