Passed
Push — master ( cb5c62...93009f )
by Radosław
02:21
created

Helper::getFieldsByCategory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Radowoj\Yaah;
4
5
use Radowoj\Yaah\Constants\SellFormOpts;
6
7
class Helper
8
{
9
    protected $client = null;
10
11 16
    public function __construct(Client $client)
12
    {
13 16
        $this->client = $client;
14 16
    }
15
16
    /**
17
     * Get simplified fields list for given category
18
     * @param  integer $idCategory id of category in question
19
     * @return array of fields metadata
20
     */
21 2
    public function getFieldsByCategory($idCategory)
22
    {
23 2
        $data = $this->client->doGetSellFormFieldsForCategory(['categoryId' => $idCategory]);
0 ignored issues
show
Documentation Bug introduced by
The method doGetSellFormFieldsForCategory does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
24
25 2
        if (!isset($data->sellFormFieldsForCategory->sellFormFieldsList->item)) {
26 1
            throw new Exception('Invalid WebAPI response: ' . print_r($data, 1));
27
        }
28
29 1
        $items = $data->sellFormFieldsForCategory->sellFormFieldsList->item;
30
31
        return array_map(function ($item) {
32
            return [
33 1
                'fid' => $item->sellFormId,
34 1
                'title' => $item->sellFormTitle,
35 1
                'required' => ($item->sellFormOpt == SellFormOpts::SELL_FORM_OPT_REQUIRED),
36 1
                'options' => $item->sellFormOptsValues,
37 1
                'optionsDesc' => $item->sellFormDesc,
38 1
            ];
39 1
        }, $items);
40
    }
41
42
    /**
43
     * Create new auction
44
     *
45
     * @throws Exception on failure
46
     *
47
     * @param  Auction $auction Auction to create
48
     * @param integer $localId - local item id, required by WebAPI
49
     * @return integer id of created auction
50
     */
51 2
    public function newAuction(AuctionInterface $auction, $localId)
52
    {
53 2
        $auctionArray = $auction->toApiRepresentation();
54 2
        $auctionArray['localId'] = $localId;
55
56 2
        $resultNewAuction = $this->client->doNewAuctionExt($auctionArray);
0 ignored issues
show
Documentation Bug introduced by
The method doNewAuctionExt does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Unused Code introduced by
$resultNewAuction is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57 2
        $resultVerify = $this->client->doVerifyItem(['localId' => $localId]);
0 ignored issues
show
Documentation Bug introduced by
The method doVerifyItem does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
58
59 2
        if (!is_object($resultVerify) || !isset($resultVerify->itemId)) {
60 1
            throw new Exception("Auction has not been created: " . print_r($resultVerify, 1));
61
        }
62
63 1
        return $resultVerify->itemId;
64
    }
65
66
67
    /**
68
     * Finish a single auction
69
     * @param  integer $auctionId itemId of auction to finish
70
     * @param  integer $cancelAllBids whether to cancel all bids
71
     * @param  string  $finishCancelReason reason
72
     * @return array
73
     */
74 4
    public function finishAuction($auctionId, $cancelAllBids = 0, $finishCancelReason = '')
75
    {
76 4
        return $this->finishAuctions((array)($auctionId), $cancelAllBids, $finishCancelReason);
77
    }
78
79
80
    /**
81
     * Finish a single auction
82
     * @param  array $auctionIds itemIds of auctions to finish
83
     * @param  integer $cancelAllBids whether to cancel all bids
84
     * @param  string  $finishCancelReason reason
85
     * @return array
86
     */
87
    public function finishAuctions(array $auctionIds, $cancelAllBids = 0, $finishCancelReason = '')
88
    {
89 8
        $finishItemsList = array_map(function ($auctionId) use ($cancelAllBids, $finishCancelReason) {
90
            return [
91 8
                'finishItemId' => $auctionId,
92 8
                'finishCancelAllBids' => $cancelAllBids,
93 8
                'finishCancelReason' => $finishCancelReason,
94 8
            ];
95 8
        }, $auctionIds);
96
97 8
        return $this->client->doFinishItems(['finishItemsList' => $finishItemsList]);
0 ignored issues
show
Documentation Bug introduced by
The method doFinishItems does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
98
    }
99
100
    /**
101
     * Change quantity of items available in auction
102
     * @param  integer $auctionId   itemId of auction to change quantity
103
     * @param  integer $newQuantity new quantity to set
104
     * @return array
105
     */
106 1
    public function changeQuantity($auctionId, $newQuantity)
107
    {
108 1
        return $this->client->doChangeQuantityItem([
0 ignored issues
show
Documentation Bug introduced by
The method doChangeQuantityItem does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
109 1
            'itemId' => $auctionId,
110
            'newItemQuantity' => $newQuantity
111 1
        ]);
112
    }
113
114
115
    /**
116
     * Return site journal deals
117
     * @param  integer $journalStart start point (dealEventId)
118
     * @return array
119
     */
120
    public function getSiteJournalDeals($journalStart)
121
    {
122
        $response = $this->client->doGetSiteJournalDeals(['journalStart' => $journalStart]);
0 ignored issues
show
Documentation Bug introduced by
The method doGetSiteJournalDeals does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
123
        if (isset($response->siteJournalDeals->item)) {
124
            return $response->siteJournalDeals->item;
125
        }
126
127
        throw new Exception("Unable to get site journal deals: " . print_r($response, 1));
128
    }
129
130
131
    /**
132
     * @param  integer $itemId id of auction to get
133
     * @return Auction | null
134
     */
135
    public function getAuctionByItemId($itemId)
136
    {
137
        $response =  $this->client->doGetItemFields(['itemId' => $itemId]);
0 ignored issues
show
Documentation Bug introduced by
The method doGetItemFields does not exist on object<Radowoj\Yaah\Client>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
138
        if (isset($response->itemFields->item)) {
139
            $auction = new Auction();
140
            $auction->fromApiRepresentation($response->itemFields->item);
141
            return $auction;
142
        }
143
144
        return null;
145
    }
146
147
148
    /**
149
     * Directly call WebAPI method (prefixed by "do")
150
     * @param  string $name method name
151
     * @param  [type] $args method arguments
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" 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...
152
     * @return [type]       [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" 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...
153
     */
154 3
    public function __call($name, $args)
155
    {
156 3
        if (strpos($name, 'do') !== 0) {
157 1
            throw new Exception("Method {$name} is not implemented in " . get_class($this));
158
        }
159
160 2
        return call_user_func_array([$this->client, $name], $args);
161
    }
162
163
}
164