Completed
Pull Request — master (#87)
by Tim
11:40
created

Application::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Application
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-cli-simple
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Cli;
22
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
25
26
/**
27
 * The M2IF - Console Tool implementation.
28
 *
29
 * This is a example console tool implementation that should give developers an impression
30
 * on how the M2IF could be used to implement their own Magento 2 importer.
31
 *
32
 * @author    Tim Wagner <[email protected]>
33
 * @copyright 2016 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/techdivision/import-cli-simple
36
 * @link      http://www.techdivision.com
37
 */
38
class Application extends \Symfony\Component\Console\Application implements ContainerAwareInterface
39
{
40
41
    /**
42
     * Regex to read the actual version number from the .semver file.
43
     *
44
     * @var string
45
     */
46
    const REGEX = "/^\-\-\-\n:major:\s(0|[1-9]\d*)\n:minor:\s(0|[1-9]\d*)\n:patch:\s(0|[1-9]\d*)\n:special:\s'([a-zA-z0-9]*\.?(?:0|[1-9]\d*)?)'\n:metadata:\s'((?:0|[1-9]\d*)?(?:\.[a-zA-z0-9\.]*)?)'/";
47
48
    /**
49
     * The application name.
50
     *
51
     * @var string
52
     */
53
    protected $name = 'M2IF - Simple Console Tool';
54
55
    /**
56
     * The DI container instance.
57
     *
58
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
59
     */
60
    protected $container;
61
62
    /**
63
     * The constructor to initialize the instance.
64
     *
65
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance
66
     */
67
    public function __construct(ContainerInterface $container)
68
    {
69
70
        // invoke the parent constructor
71
        parent::__construct($this->name, vsprintf('%d.%d.%d-%s', $this->parse(dirname(__DIR__) . DIRECTORY_SEPARATOR . '.semver')));
72
73
        // set the DI container instance
74
        $this->setContainer($container);
75
    }
76
    /**
77
     * Sets the container.
78
     *
79
     * @param \Symfony\Component\DependencyInjection\ContainerInterface|null $container The DI container instance
80
     *
81
     * @return void
82
     */
83
    public function setContainer(ContainerInterface $container = null)
84
    {
85
        $this->container = $container;
86
    }
87
88
    /**
89
     * Return's the DI container instance.
90
     *
91
     * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance
92
     */
93
    public function getContainer()
94
    {
95
        return $this->container;
96
    }
97
98
    /**
99
     * Parse and return the version number from the application's .semver file.
100
     *
101
     * @param string $semverFile The path to the semver file
102
     *
103
     * @return array The array with the version information
104
     * @throws \Exception Is thrown, if the .semver file is not available or invalid
105
     */
106
    protected function parse($semverFile)
107
    {
108
109
        // load the content of the semver file
110
        $output = file_get_contents($semverFile);
111
112
        // extract the version information
113
        if (!preg_match_all(self::REGEX, $output, $matches)) {
114
            throw new \Exception($this, 'Bad semver file.');
115
        }
116
117
        // prepare and return the version number
118
        list(, $major, $minor, $patch, $special, $metadata) = array_map('current', $matches);
119
        return compact('major', 'minor', 'patch', 'special', 'metadata');
120
    }
121
}
122