Helper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 31
c 1
b 0
f 0
dl 0
loc 57
ccs 27
cts 27
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A createItemDataForUpdate() 0 40 5
1
<?php
2
3
/*
4
 * This file is part of michaelbutler/phposh.
5
 * Source: https://github.com/michaelbutler/phposh
6
 *
7
 * (c) Michael Butler <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file named LICENSE.
11
 */
12
13
namespace PHPosh\Provider\Poshmark;
14
15
/**
16
 * Utility/helper functions for Poshmark Service.
17
 */
18
class Helper
19
{
20
    /**
21
     * Take an array of user itemFields and rawItemData from Poshmark, and generate a valid array that may be used
22
     * in a POST update for editing that item (which will later be JSONified)
23
     * $itemFields should be an array like:
24
     * [
25
     *     'title' => 'New title',
26
     *     'description' => 'New description',
27
     *     'price' => '4.95 USD', // Price, with currency code (will default to USD)
28
     *                            // a Price object is also supported
29
     *     'brand' => 'Nike', // brand name
30
     * ]
31
     * The fields are all optional, only requested fields will be edited. However you must supply at least one.
32
     *
33
     * @return array map of key=value pairs that can be sent as the POST body for an update item request
34
     */
35 3
    public static function createItemDataForUpdate(array $itemFields, array $rawItemData): array
36
    {
37 3
        $colors = [];
38 3
        foreach ($rawItemData['colors'] as $arr) {
39 3
            $colors[] = $arr['name'];
40
        }
41
42 3
        if (isset($itemFields['price'])) {
43 2
            $newPrice = $itemFields['price'] instanceof Price ?
44 1
                $itemFields['price'] :
45 2
                Price::fromString($itemFields['price']);
46
47
            $newPrice = [
48 2
                'val' => $newPrice->getAmount(),
49 2
                'currency_code' => $newPrice->getCurrencyCode(),
50 2
                'currency_symbol' => $newPrice->getCurrencySymbol(),
51
            ];
52
        } else {
53 1
            $newPrice = $rawItemData['price_amount'];
54
        }
55
56 3
        $newTitle = $itemFields['title'] ?? $rawItemData['title'];
57 3
        $newDesc = $itemFields['description'] ?? $rawItemData['description'];
58 3
        $newBrand = $itemFields['brand'] ?? $rawItemData['brand'];
59
60
        return [
61 3
            'catalog' => $rawItemData['catalog'],
62 3
            'colors' => $colors,
63 3
            'inventory' => $rawItemData['inventory'],
64 3
            'price_amount' => $newPrice,
65 3
            'original_price_amount' => $rawItemData['original_price_amount'],
66 3
            'title' => $newTitle,
67 3
            'description' => $newDesc,
68 3
            'brand' => $newBrand,
69 3
            'condition' => $rawItemData['condition'],
70
            'cover_shot' => [
71 3
                'id' => $rawItemData['cover_shot']['id'],
72
            ],
73 3
            'pictures' => $rawItemData['pictures'] ?: [], // TODO make this work right
74 3
            'seller_private_info' => $rawItemData['seller_private_info'] ?? new \stdClass(),
75
        ];
76
    }
77
}
78