Completed
Push — 3.0 ( 46111f...d5ff83 )
by Daniel
02:06
created

DeployManager::setSortPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 *
4
 *
5
 *
6
 *
7
 */
8
9
namespace MagentoHackathon\Composer\Magento;
10
11
use Composer\IO\IOInterface;
12
use MagentoHackathon\Composer\Magento\Deploy\Manager\Entry;
13
use MagentoHackathon\Composer\Magento\Deploystrategy\Copy;
14
use MagentoHackathon\Composer\Magento\Event\EventManager;
15
use MagentoHackathon\Composer\Magento\Event\PackageDeployEvent;
16
17
class DeployManager
18
{
19
20
    const SORT_PRIORITY_KEY = 'magento-deploy-sort-priority';
21
22
    /**
23
     * @var Entry[]
24
     */
25
    protected $packages = array();
26
27
    /**
28
     * @var IOInterface
29
     */
30
    protected $io;
31
32
    /**
33
     * an array with package names as key and priorities as value
34
     *
35
     * @var array
36
     */
37
    protected $sortPriority = array();
38
39
    /**
40
     * @var EventManager
41
     */
42
    protected $eventManager;
43
44
    /**
45
     * @param EventManager $eventManager
46
     */
47 6
    public function __construct(EventManager $eventManager)
48
    {
49 6
        $this->eventManager = $eventManager;
50 6
    }
51
52
    /**
53
     * @param Entry $package
54
     */
55 1
    public function addPackage(Entry $package)
56
    {
57 1
        $this->packages[] = $package;
58 1
    }
59
60
    /**
61
     * @param $priorities
62
     */
63 5
    public function setSortPriority($priorities)
64
    {
65 5
        $this->sortPriority = $priorities;
66 5
    }
67
68
    /**
69
     * uses the sortPriority Array to sort the packages.
70
     * Highest priority first.
71
     * Copy gets per default higher priority then others
72
     */
73 1
    protected function sortPackages()
74
    {
75 1
        $sortPriority = $this->sortPriority;
76
        $getPriorityValue = function (Entry $object) use ($sortPriority) {
77 1
            $result = 100;
78 1
            if (isset($sortPriority[$object->getPackageName()])) {
79
                $result = $sortPriority[$object->getPackageName()];
80 1
            } elseif ($object->getDeployStrategy() instanceof Copy) {
81
                $result = 101;
82
            }
83 1
            return $result;
84 1
        };
85 1
        usort(
86 1
            $this->packages,
87 1
            function ($a, $b) use ($getPriorityValue) {
88
                /** @var Entry $a */
89
                /** @var Entry $b */
90 1
                $aVal = $getPriorityValue($a);
91 1
                $bVal = $getPriorityValue($b);
92 1
                if ($aVal == $bVal) {
93 1
                    return 0;
94
                }
95
                return ($aVal > $bVal) ? -1 : 1;
96
            }
97 1
        );
98 1
    }
99
100
    /**
101
     * Deploy all the queued packages
102
     */
103 1
    public function doDeploy()
104
    {
105 1
        $this->sortPackages();
106
        /** @var Entry $package */
107 1
        foreach ($this->packages as $package) {
108 1
            $this->eventManager->dispatch(new PackageDeployEvent('pre-package-deploy', $package));
109 1
            $package->getDeployStrategy()->deploy();
110 1
            $this->eventManager->dispatch(new PackageDeployEvent('post-package-deploy', $package));
111 1
        }
112 1
    }
113
114
    /**
115
     * @return Deploy\Manager\Entry[]
116
     */
117
    public function getEntries()
118
    {
119
        return $this->packages;
120
    }
121
}
122