ProductService::addProductVariants()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 2
1
<?php
2
/*
3
 * This file is part of the MailChimpEcommerceBundle package.
4
 *
5
 * Copyright (c) 2017 kevin92dev.es
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * Feel free to edit as you please, and have fun.
11
 *
12
 * @author Kevin Murillo <[email protected]>
13
 */
14
15
namespace Kevin92dev\MailChimpEcommerceBundle\Services;
16
17
use Kevin92dev\MailChimpEcommerceBundle\Entities\Product;
18
use Kevin92dev\MailChimpEcommerceBundle\Entities\ProductVariant;
19
use Kevin92dev\MailChimpEcommerceBundle\Exceptions\ProductNotFoundException;
20
use Kevin92dev\MailChimpEcommerceBundle\RequestTypes;
21
22
/**
23
 * ProductService
24
 *
25
 * @author Kevin Murillo <[email protected]>
26
 */
27
class ProductService
28
{
29
    /**
30
     * @var MailChimp
31
     */
32
    private $mailChimp;
33
34
    /**
35
     * Initializes ProductService
36
     *
37
     * @param MailChimp $mailChimp
38
     */
39
    public function __construct(MailChimp $mailChimp)
40
    {
41
        $this->mailChimp = $mailChimp;
42
    }
43
44
    /**
45
     * Create a new product in MailChimp
46
     *
47
     * @var Product $product
48
     */
49 View Code Duplication
    public function create(Product $product)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $method = RequestTypes::$POST;
52
        $resource = '/products';
53
54
        $body = [
55
            'id' => strval($product->getId()),
56
            'title' => strval($product->getTitle()),
57
            'handle' => strval($product->getHandle()),
58
            'url' => strval($product->getUrl()),
59
            'description' => strval($product->getDescription()),
60
            'type' => strval($product->getType()),
61
            'vendor' => strval($product->getVendor()),
62
            'image_url' => strval($product->getImageUrl())
63
        ];
64
65
        $this->addProductVariants($product, $body);
66
67
        $this->mailChimp->doRequest($method, $body, $resource);
68
    }
69
70
    /**
71
     * Edit a product in MailChimp
72
     *
73
     * @var Product $product
74
     */
75 View Code Duplication
    public function edit(Product $product)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $method = RequestTypes::$PATCH;
78
        $resource = '/products/'.$product->getId();
79
80
        $body = [
81
            'title' => strval($product->getTitle()),
82
            'handle' => strval($product->getHandle()),
83
            'url' => strval($product->getUrl()),
84
            'description' => strval($product->getDescription()),
85
            'type' => strval($product->getType()),
86
            'vendor' => strval($product->getVendor()),
87
            'image_url' => strval($product->getImageUrl())
88
        ];
89
90
        $this->addProductVariants($product, $body);
91
92
        $this->mailChimp->doRequest($method, $body, $resource);
93
    }
94
95
    /**
96
     * Read products from MailChimp
97
     *
98
     * @param Product $product
99
     * @param int $count
100
     * @param int $offset
101
     *
102
     * @return null|string
103
     */
104 View Code Duplication
    public function read(Product $product = null, $count = 50, $offset = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $method = RequestTypes::$GET;
107
108
        if ($product instanceof Product) {
109
            $resource = '/products/'.$product->getId();
110
        } else {
111
            $resource = '/products';
112
        }
113
114
        $resource .= '?count='.$count.'&offset='.$offset;
115
116
        $data = [];
117
118
        try {
119
            $request = $this->mailChimp->doRequest($method, $data, $resource);
120
        } catch (ProductNotFoundException $e) {
121
            return null;
122
        }
123
124
        return $request->getBody()->getContents();
125
    }
126
127
    /**
128
     * Delete product from MailChimp
129
     *
130
     * @var Product $product
131
     */
132
    public function delete(Product $product)
133
    {
134
        $method = RequestTypes::$DELETE;
135
        $resource = '/products/'.$product->getId();
136
137
        $this->mailChimp->doRequest($method, null, $resource);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
    }
139
140
    /**
141
     * Add product variants to array
142
     *
143
     * @param Product $product
144
     * @param array   $body
145
     */
146
    public function addProductVariants(Product $product, &$body)
147
    {
148
        /**
149
         * @var ProductVariant $productVariant
150
         */
151
        foreach ($product->getProductVariants() as $productVariant) {
152
            $body['variants'][] = [
153
                'id' => strval($productVariant->getId()),
154
                'title' => strval($productVariant->getTitle()),
155
                'url' => strval($productVariant->getUrl()),
156
                'sku' => strval($productVariant->getSku()),
157
                'price' => floatval($productVariant->getPrice()),
158
                'inventory_quantity' => intval($productVariant->getInventoryQuantity()),
159
                'image_url' => strval($productVariant->getImageUrl()),
160
                'backorders' => strval($productVariant->getBackorders()),
161
                'visibility' => strval($productVariant->getVisibility())
162
            ];
163
        }
164
    }
165
}