Completed
Push — master ( ca41aa...5a5b4d )
by Tim
15s
created

Application::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.0116
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 1
    public function __construct(ContainerInterface $container)
68
    {
69
70
        // set the DI container instance
71 1
        $this->setContainer($container);
72
73
        // invoke the parent constructor
74 1
        parent::__construct($this->name, vsprintf('%d.%d.%d-%s', $this->parse(dirname(__DIR__) . DIRECTORY_SEPARATOR . '.semver')));
75 1
    }
76
77
    /**
78
     * Sets the DI container instance.
79
     *
80
     * @param \Symfony\Component\DependencyInjection\ContainerInterface|null $container The DI container instance
81
     *
82
     * @return void
83
     */
84 1
    public function setContainer(ContainerInterface $container = null)
85
    {
86 1
        $this->container = $container;
87 1
    }
88
89
    /**
90
     * Return's the DI container instance.
91
     *
92
     * @return \Symfony\Component\DependencyInjection\ContainerInterface|null The DI container instance
93
     */
94 1
    public function getContainer()
95
    {
96 1
        return $this->container;
97
    }
98
99
    /**
100
     * Parse and return the version number from the application's .semver file.
101
     *
102
     * @param string $semverFile The path to the semver file
103
     *
104
     * @return array The array with the version information
105
     * @throws \Exception Is thrown, if the .semver file is not available or invalid
106
     */
107 1
    protected function parse($semverFile)
108
    {
109
110
        // load the content of the semver file
111 1
        $output = file_get_contents($semverFile);
112
113
        // initialize the array with the matches
114 1
        $matches = array();
115
116
        // extract the version information
117 1
        if (!preg_match_all(self::REGEX, $output, $matches)) {
118
            throw new \Exception($this, 'Bad semver file.');
119
        }
120
121
        // prepare and return the version number
122 1
        list(, $major, $minor, $patch, $special, $metadata) = array_map('current', $matches);
123 1
        return compact('major', 'minor', 'patch', 'special', 'metadata');
124
    }
125
}
126