Completed
Push — master ( 9cbce3...db002c )
by Tim
10s
created

Application::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 6
cts 7
cp 0.8571
rs 9.6666
c 0
b 0
f 0
cc 2
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
        // initialize the variables for the elements of the version string
74 1
        $major = 1;
75 1
        $minor = 0;
76 1
        $patch = 0;
77 1
        $special = null;
78 1
        $metadata = null;
79
80
        // parse the file with the semantic versioning data
81 1
        extract($this->parse(dirname(__DIR__) . DIRECTORY_SEPARATOR . '.semver'));
0 ignored issues
show
Bug introduced by
$this->parse(dirname(__D..._SEPARATOR . '.semver') cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
82
83
        // invoke the parent constructor
84 1
        parent::__construct($this->name, sprintf('%d.%d.%d', $major, $minor, $patch) . ($special ? sprintf('-%s%s', $special, $metadata) : null));
85 1
    }
86
87
    /**
88
     * Sets the DI container instance.
89
     *
90
     * @param \Symfony\Component\DependencyInjection\ContainerInterface|null $container The DI container instance
91
     *
92
     * @return void
93
     */
94 1
    public function setContainer(ContainerInterface $container = null)
95
    {
96 1
        $this->container = $container;
97 1
    }
98
99
    /**
100
     * Return's the DI container instance.
101
     *
102
     * @return \Symfony\Component\DependencyInjection\ContainerInterface|null The DI container instance
103
     */
104 1
    public function getContainer()
105
    {
106 1
        return $this->container;
107
    }
108
109
    /**
110
     * Parse and return the version number from the application's .semver file.
111
     *
112
     * @param string $semverFile The path to the semver file
113
     *
114
     * @return array The array with the version information
115
     * @throws \Exception Is thrown, if the .semver file is not available or invalid
116
     */
117 1
    protected function parse($semverFile)
118
    {
119
120
        // load the content of the semver file
121 1
        $output = file_get_contents($semverFile);
122
123
        // initialize the array with the matches
124 1
        $matches = array();
125
126
        // extract the version information
127 1
        if (!preg_match_all(self::REGEX, $output, $matches)) {
128
            throw new \Exception($this, 'Bad semver file.');
129
        }
130
131
        // prepare and return the version number
132 1
        list(, $major, $minor, $patch, $special, $metadata) = array_map('current', $matches);
133 1
        return compact('major', 'minor', 'patch', 'special', 'metadata');
134
    }
135
}
136