Auction::__construct()   A
last analyzed

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
/**
9
 * Representation of Allegro Auction (with form field values), used by Helper class to create an auction or retrieve its data.
10
 */
11
class Auction implements AuctionInterface
12
{
13
    const MAX_PHOTOS = 8;
14
15
    protected $fields = [];
16
17
    protected $photos = [];
18
19 18
    public function __construct(array $fields = [])
20
    {
21 18
        $this->fromArray($fields);
22 18
    }
23
24
25
    /**
26
     * Sets photos for auction
27
     * @param array $photos array of photo file paths
28
     */
29 3
    public function setPhotos(array $photos)
30
    {
31 3
        $photosCount = count($photos);
32
33 3
        if ($photosCount > self::MAX_PHOTOS) {
34 1
            throw new InvalidArgumentException("Photo files limit exceeded, " . self::MAX_PHOTOS . " allowed, " . $photosCount . " given");
35
        }
36
37 2
        $this->photos = $photos;
38 2
    }
39
40
41
    /**
42
     * Populate from array
43
     * @param  array $fields (WebAPI fid => field value)
44
     * @see Radowoj\Yaah\Decorators\AuctionArrayMapDecorator for an interface more friendly to programmer's sanity :)
45
     */
46 15
    public function fromArray(array $fields)
47
    {
48 15
        $this->fields = $fields;
49 15
    }
50
51
52
    /**
53
     * Returns WebAPI's representation of an auction (array of fields for doNewAuctionExt())
54
     * @return array
55
     */
56 5
    public function toApiRepresentation()
57
    {
58 5
        $fields = [];
59
60 5
        foreach ($this->fields as $fid => $value) {
61 5
            $fields[] = (new Field($fid, $value))->toArray();
62 5
        }
63
64 5
        $this->addPhotoFields($fields);
65
66
        return [
67 4
            'fields' => $fields,
68 4
        ];
69
    }
70
71
72
    /**
73
     * Creates an auction from WebAPI's representation (array of fields from doGetItemFields)
74
     * @param  array  $fields
75
     */
76 2
    public function fromApiRepresentation(array $fields)
77
    {
78 2
        $this->fields = [];
79 2
        $this->photos = [];
80
81 2
        foreach ($fields as $apiField) {
82 1
            $field = new Field();
83 1
            $field->fromArray((array)$apiField);
84 1
            $this->fields[$field->getFid()] = $field->getValue();
85 2
        }
86 2
    }
87
88
89
    /**
90
     * Add photos to given array of Fields
91
     * @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...
92
     */
93 7
    protected function addPhotoFields(array& $fields)
94
    {
95 7
        $count = count($this->photos);
96 7
        if (!$count) {
97 5
            return;
98
        }
99
100 2
        $index = 0;
101 2
        foreach ($this->photos as $photo) {
102 2
            if (!is_readable($photo)) {
103 1
                throw new Exception("Photo file is not readable: {$photo}");
104
            }
105
106 1
            $fields[] = (new Field(AuctionFids::FID_PHOTO + $index, file_get_contents($photo), Field::VALUE_IMAGE))->toArray();
107 1
            $index++;
108 1
        }
109 1
    }
110
111
112
    /**
113
     * Simplified array representation (similar to constructor params)
114
     * @return array
115
     */
116 2
    public function toArray()
117
    {
118 2
        $fields = $this->fields;
119 2
        $this->addPhotoFields($fields);
120 2
        return $fields;
121
    }
122
123
}
124