Completed
Push — master ( 062442...f19283 )
by Dan
02:25
created

PalettesBuilder::getRandomPalette()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 14
nc 9
nop 1
crap 5
1
<?php
2
3
namespace SixtyNine\Cloud\Builder;
4
5
use SixtyNine\Cloud\Model\Palette;
6
use Symfony\Component\Yaml\Yaml;
7
8
class PalettesBuilder
9
{
10
    protected $palettes = array();
11
12 3
    protected function __construct()
13
    {
14 3
    }
15
16 3
    public static function create()
17
    {
18 3
        return new self();
19
    }
20
21
    /**
22
     * @param string $name
23
     * @param array $colors
24
     * @return PalettesBuilder
25
     */
26
    public function addPalette($name, array $colors)
27
    {
28
        $palette = new Palette();
29
        $palette->setName($name)->setColors($colors);
0 ignored issues
show
Documentation introduced by
$colors is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
        $this->palettes[$name] = $palette;
31
32
        return $this;
33
    }
34
35
    /**
36
     * @param string $file
37
     * @return PalettesBuilder
38
     * @throws \InvalidArgumentException
39
     */
40
    public function importPalettes($file)
41
    {
42
        if (!file_exists($file)) {
43
            throw new \InvalidArgumentException('File not found: ' . $file);
44
        }
45
46
        $yml = Yaml::parse(file_get_contents($file));
47
48
        if (!array_key_exists('palettes', $yml)) {
49
            throw new \InvalidArgumentException('Invalid palettes YAML');
50
        }
51
52
        foreach ($yml['palettes'] as $name => $colors) {
53
            $this->addPalette($name, $colors);
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $name
61
     * @return Palette
62
     * @throws \InvalidArgumentException
63
     */
64
    public function getNamedPalette($name)
65
    {
66
        if (!array_key_exists($name, $this->palettes)) {
67
            throw new \InvalidArgumentException('Palette not found: ' . $name);
68
        }
69
70
        return $this->palettes[$name];
71
    }
72
73
    /**
74
     * @param int $count
75
     * @return Palette
76
     */
77 3
    public function getRandomPalette($count = 5)
78
     {
79 3
         $colors = array();
80 3
         for ($i = 0; $i < $count; $i++) {
81 3
             $part1 = dechex(rand(0, 255));
82 3
             $part2 = dechex(rand(0, 255));
83 3
             $part3 = dechex(rand(0, 255));
84
             $color = '#'
85 3
                . (strlen($part1) >= 2 ? $part1 : '0' . $part1)
86 3
                . (strlen($part2) >= 2 ? $part2 : '0' . $part2)
87 3
                . (strlen($part3) >= 2 ? $part3 : '0' . $part3)
88
             ;
89 3
             $colors[] = $color;
90
         }
91 3
         $palette = new Palette();
92 3
         $palette->setName('random')->setColors($colors);
0 ignored issues
show
Documentation introduced by
$colors is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93 3
         return $palette;
94
     }
95
96
    /**
97
     * @param array $colors
98
     */
99
    public function getPalette(array $colors)
100
    {
101
        $palette = new Palette();
102
        $palette->setName('palette')->setColors($colors);
0 ignored issues
show
Documentation introduced by
$colors is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function getPalettes()
109
    {
110
        return $this->palettes;
111
    }
112
}
113