Arguments   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A push() 0 12 2
A finalize() 0 6 1
A validate() 0 6 2
1
<?php namespace nyx\console\input\parameter\values;
2
3
// External dependencies
4
use nyx\core;
5
6
// Internal dependencies
7
use nyx\console\input\parameter;
8
use nyx\console\input\exceptions;
9
10
/**
11
 * Input Arguments
12
 *
13
 * @version     0.1.0
14
 * @author      Michal Chojnacki <[email protected]>
15
 * @copyright   2012-2017 Nyx Dev Team
16
 * @link        https://github.com/unyx/nyx
17
 */
18
class Arguments extends parameter\Values
19
{
20
    /**
21
     * @var parameter\definitions\Arguments   The Definitions of the Arguments that can be present in this collection.
22
     */
23
    protected $definitions;
24
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * Overridden to enforce a stricter Definitions collection type.
29
     */
30
    public function __construct(parameter\definitions\Arguments $definition)
31
    {
32
        parent::__construct($definition);
33
    }
34
35
    /**
36
     * Adds an argument's value to the collection.
37
     *
38
     * Automatically decides on the name of the argument based on the present definition.
39
     *
40
     * @param   string  $value                  The argument's value to set.
41
     * @param   $this
42
     * @throws  exceptions\ArgumentsTooMany     When the definition does not permit any further arguments.
43
     */
44
    public function push(string $value) : core\collections\interfaces\Map
45
    {
46
        // Grab an Argument for the next index. If we get null here, it means there are no further Arguments
47
        // that accept values present.
48
        if (!$argument = $this->definitions->getNextDefinition($this)) {
49
            throw new exceptions\ArgumentsTooMany($this);
50
        }
51
52
        // Now that we know how to map the Argument via its name, we can safely set its value. Set() will also
53
        // resolve the case of parameters accepting multiple values.
54
        return $this->set($argument->getName(), $value);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * Overridden to include validation in the finalize step, while ensuring the Collection is valid
61
     * already before being populated with default values for not explicitly set Arguments.
62
     */
63
    public function finalize() : parameter\Values
64
    {
65
        $this->validate();
66
67
        return parent::finalize();
68
    }
69
70
    /**
71
     * Validates this collection.
72
     *
73
     * Checks if the Collection contains all necessary arguments. We are not validating whether there are
74
     * too many arguments as this can only happen when push()'ing values directly, which is handled inside
75
     * that method as well.
76
     *
77
     * @throws  exceptions\ArgumentsNotEnough   When not enough arguments are present in this Collection.
78
     */
79
    protected function validate()
80
    {
81
        if ($this->count() < $this->definitions->required()) {
82
            throw new exceptions\ArgumentsNotEnough($this);
83
        }
84
    }
85
}
86