Passed
Push — master ( 18fd6e...a300cb )
by Radosław
02:30
created

Auction::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use Radowoj\Yaah\Constants\AuctionFids;
6
7
class Auction
8
{
9
    const MAX_PHOTOS = 8;
10
11
    protected $localId = null;
12
13
    protected $fields = [];
14
15
    protected $photos = [];
16
17
    public function __construct($localId, array $fields)
18
    {
19
        $this->localId = $localId;
20
        $this->fields = $fields;
21
    }
22
23
    public function setPhotos(array $photos)
24
    {
25
        $photosCount = count($photos);
26
27
        if ($photosCount > self::MAX_PHOTOS) {
28
            throw new Exception("Photo files limit exceeded, " . self::MAX_PHOTOS . " allowed, " . $photosCount . " given");
29
        }
30
31
        $this->photos = $photos;
32
    }
33
34
    public function getApiRepresentation()
35
    {
36
        $fields = [];
37
38
        foreach($this->fields as $fid => $value) {
39
            $fields[] = (new Field($fid, $value))->toArray();
40
        }
41
42
        $this->addPhotoFields($fields);
43
44
        return [
45
            'fields' => $fields,
46
            'localId' => $this->getLocalId(),
47
        ];
48
    }
49
50
51
    protected function addPhotoFields(array& $fields)
52
    {
53
        $count = count($this->photos);
54
        if (!$count) {
55
            return;
56
        }
57
58
        $index = 0;
59
        foreach ($this->photos as $photo) {
60
            if (!is_readable($photo)) {
61
                throw new Exception("Photo file {$photo} is not readable");
62
            }
63
64
            $fields[] = (new Field(AuctionFids::FID_PHOTO + $index, file_get_contents($photo), Field::VALUE_IMAGE))->toArray();
65
            $index++;
66
        }
67
    }
68
69
70
    public function getLocalId()
71
    {
72
        return $this->localId;
73
    }
74
75
76
}
77