VersionnedBundle   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 15
c 6
b 0
f 2
lcom 1
cbo 3
dl 0
loc 116
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
D boot() 0 33 10
A getVersion() 0 4 1
A setNeedInstallation() 0 5 1
A getNeedInstallation() 0 4 1
A setNeedUpToDate() 0 5 1
A getNeedUpToDate() 0 4 1
1
<?php
2
3
namespace kujaff\VersionsBundle\Model;
4
5
use Symfony\Component\HttpKernel\Bundle\Bundle;
6
use kujaff\VersionsBundle\Exception\VersionException;
7
8
/**
9
 * Extends this class instead of Bundle SF2 class to add getVersion
10
 */
11
class VersionnedBundle extends Bundle
12
{
13
    /**
14
     * Version
15
     *
16
     * @var Version
17
     */
18
    protected $version;
19
20
    /**
21
     * Indicate if bundle needs to be installed or if it can be used without installation
22
     *
23
     * @var boolean
24
     */
25
    protected $needInstallation = true;
26
27
    /**
28
     * Indicate if bundle needs to be up to date or if it can be used without being up to date
29
     *
30
     * @var boolean
31
     */
32
    protected $needUpToDate = true;
33
34
    /**
35
     * Boot the bundle
36
     *
37
     * @throws Exception
38
     */
39
    public function boot()
40
    {
41
        parent::boot();
42
43
        if (php_sapi_name() == 'cli') {
44
            return null;
45
        }
46
47
        $bundleVersion = null;
48
49
        // need to be installed
50
        if ($this->getNeedInstallation() && $this->container->getParameter('versions.checkNeedInstallation')) {
51
            $bundleVersion = $this->container->get('versions.bundle')->getVersion($this->getName());
52
            if ($bundleVersion->isInstalled() === false) {
53
                if ($this->getName() == 'VersionsBundle') {
54
                    $message = 'Bundle "' . $this->getName() . '" needs to be installed. Exec "php app/console bundle:install ' . $this->getName() . ' --force".';
55
                } else {
56
                    $message = 'Bundle "' . $this->getName() . '" needs to be installed. Exec "php app/console bundle:install ' . $this->getName() . '".';
57
                }
58
                throw new VersionException($message);
59
            }
60
        }
61
62
        // need to be updated
63
        if ($this->getNeedUpToDate() && $this->container->getParameter('versions.checkNeedUpToDate')) {
64
            if ($bundleVersion === null) {
65
                $bundleVersion = $this->container->get('versions.bundle')->getVersion($this->getName());
66
            }
67
            if ($bundleVersion->needUpdate()) {
68
                throw new VersionException('Bundle "' . $this->getName() . '" needs to be updated. Exec "php app/console bundle:update ' . $this->getName() . '".');
69
            }
70
        }
71
    }
72
73
    /**
74
     * Return version
75
     *
76
     * @return Version
77
     */
78
    public function getVersion()
79
    {
80
        return $this->version;
81
    }
82
83
    /**
84
     * Define if bundle needs to be installed
85
     *
86
     * @param boolean $needInstallation
87
     * @return VersionnedBundle
88
     */
89
    public function setNeedInstallation($needInstallation)
90
    {
91
        $this->needInstallation = $needInstallation;
92
        return $this;
93
    }
94
95
    /**
96
     * Get if bundle needs to be installed
97
     *
98
     * @return boolean
99
     */
100
    public function getNeedInstallation()
101
    {
102
        return $this->needInstallation;
103
    }
104
105
    /**
106
     * Define if bundle needs to be up-to-date
107
     *
108
     * @param boolean $needUpToDate
109
     * @return VersionnedBundle
110
     */
111
    public function setNeedUpToDate($needUpToDate)
112
    {
113
        $this->needUpToDate = $needUpToDate;
114
        return $this;
115
    }
116
117
    /**
118
     * Get if bundle needs to be up-to-date
119
     *
120
     * @return boolean
121
     */
122
    public function getNeedUpToDate()
123
    {
124
        return $this->needUpToDate;
125
    }
126
}
127