Completed
Pull Request — master (#6)
by Dan
07:19
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
use Webmozart\Assert\Assert;
8
9
class PalettesBuilder
10
{
11
    protected $palettes = array();
12
13 3
    protected function __construct()
14
    {
15 3
    }
16
17 3
    public static function create()
18
    {
19 3
        return new self();
20
    }
21
22
    /**
23
     * @param string $name
24
     * @param array $colors
25
     * @return PalettesBuilder
26
     */
27
    public function addPalette($name, array $colors)
28
    {
29
        $palette = new Palette();
30
        $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...
31
        $this->palettes[$name] = $palette;
32
33
        return $this;
34
    }
35
36
    /**
37
     * @param string $file
38
     * @return PalettesBuilder
39
     * @throws \InvalidArgumentException
40
     */
41
    public function importPalettes($file)
42
    {
43
        Assert::fileExists($file, 'File not found: ' . $file);
44
45
        $yml = Yaml::parse(file_get_contents($file));
46
47
        Assert::keyExists($yml, 'palettes', 'Invalid palettes YAML');
48
49
        foreach ($yml['palettes'] as $name => $colors) {
50
            $this->addPalette($name, $colors);
51
        }
52
53
        return $this;
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return Palette
59
     * @throws \InvalidArgumentException
60
     */
61
    public function getNamedPalette($name)
62
    {
63
        Assert::keyExists($this->palettes, $name, 'Palette not found: ' . $name);
64
        return $this->palettes[$name];
65
    }
66
67
    /**
68
     * @param int $count
69
     * @return Palette
70
     */
71 3
    public function getRandomPalette($count = 5)
72
     {
73 3
         $colors = array();
74 3
         for ($i = 0; $i < $count; $i++) {
75 3
             $part1 = dechex(rand(0, 255));
76 3
             $part2 = dechex(rand(0, 255));
77 3
             $part3 = dechex(rand(0, 255));
78
             $color = '#'
79 3
                . (strlen($part1) >= 2 ? $part1 : '0' . $part1)
80 3
                . (strlen($part2) >= 2 ? $part2 : '0' . $part2)
81 3
                . (strlen($part3) >= 2 ? $part3 : '0' . $part3)
82
             ;
83 3
             $colors[] = $color;
84
         }
85 3
         $palette = new Palette();
86 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...
87 3
         return $palette;
88
     }
89
90
    /**
91
     * @param array $colors
92
     */
93
    public function getPalette(array $colors)
94
    {
95
        $palette = new Palette();
96
        $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...
97
        return $palette;
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    public function getPalettes()
104
    {
105
        return $this->palettes;
106
    }
107
}
108