Passed
Push — master ( b1ea1f...f2eb6e )
by Radosław
02:44
created

Auction::setFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use Radowoj\Yaah\Constants\AuctionFids;
6
use InvalidArgumentException;
7
8
class Auction implements AuctionInterface
9
{
10
    const MAX_PHOTOS = 8;
11
12
    protected $fields = [];
13
14
    protected $photos = [];
15
16 6
    public function __construct(array $fields = [])
17
    {
18 6
        $this->fromArray($fields);
19 6
    }
20
21
22
    /**
23
     * Sets photos for auction
24
     * @param array $photos array of photo file paths
25
     */
26 3
    public function setPhotos(array $photos)
27
    {
28 3
        $photosCount = count($photos);
29
30 3
        if ($photosCount > self::MAX_PHOTOS) {
31 1
            throw new InvalidArgumentException("Photo files limit exceeded, " . self::MAX_PHOTOS . " allowed, " . $photosCount . " given");
32
        }
33
34 2
        $this->photos = $photos;
35 2
    }
36
37
38 6
    public function fromArray(array $fields)
39
    {
40 6
        $this->fields = $fields;
41 6
    }
42
43
44
    /**
45
     * Returns WebAPI's representation of an auction (array of fields)
46
     * @return array
47
     */
48 3
    public function toApiRepresentation()
49
    {
50 3
        $fields = [];
51
52 3
        foreach ($this->fields as $fid => $value) {
53 3
            $fields[] = (new Field($fid, $value))->toArray();
54 3
        }
55
56 3
        $this->addPhotoFields($fields);
57
58
        return [
59 2
            'fields' => $fields,
60 2
        ];
61
    }
62
63
64
    /**
65
     * Creates an auction from WebAPI's representation (array of fields)
66
     * @param  array  $fields
67
     */
68 1
    public function fromApiRepresentation(array $fields)
69
    {
70 1
        $this->fields = [];
71 1
        $this->photos = [];
72
73 1
        foreach ($fields as $apiField) {
74 1
            $field = new Field();
75 1
            $field->fromArray((array)$apiField);
76 1
            $this->fields[$field->getFid()] = $field->getValue();
77 1
        }
78 1
    }
79
80
81
    /**
82
     * Add photos to given array of Fields
83
     * @param array& $fields array of Fields to extend with photos
0 ignored issues
show
Documentation introduced by
The doc-type array& could not be parsed: Unknown type name "array&" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
84
     */
85 5
    protected function addPhotoFields(array& $fields)
86
    {
87 5
        $count = count($this->photos);
88 5
        if (!$count) {
89 3
            return;
90
        }
91
92 2
        $index = 0;
93 2
        foreach ($this->photos as $photo) {
94 2
            if (!is_readable($photo)) {
95 1
                throw new Exception("Photo file is not readable: {$photo}");
96
            }
97
98 1
            $fields[] = (new Field(AuctionFids::FID_PHOTO + $index, file_get_contents($photo), Field::VALUE_IMAGE))->toArray();
99 1
            $index++;
100 1
        }
101 1
    }
102
103
104
    /**
105
     * Simplified array representation (similar to constructor params)
106
     * @return array
107
     */
108 2
    public function toArray()
109
    {
110 2
        $fields = $this->fields;
111 2
        $this->addPhotoFields($fields);
112 2
        return $this->fields;
113
    }
114
115
}
116