Passed
Push — master ( a8f3a0...0e6cb9 )
by C.
02:17
created

StoresService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 127
Duplicated Lines 59.84 %

Importance

Changes 0
Metric Value
wmc 8
dl 76
loc 127
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updateStores() 22 22 2
A listStores() 12 12 1
A deleteStores() 17 17 2
A createStores() 22 22 2
A __construct() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of PHP CS Fixer.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *     Dariusz Rumiński <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Etrias\PaazlConnector\Services;
14
15
use DateTime;
16
use Etrias\PaazlConnector\Processor\Processor;
17
use Etrias\PaazlConnector\ServiceType\Service as GeneralServiceType;
18
use Etrias\PaazlConnector\StructType\ChangeStoreDetailsType;
19
use Etrias\PaazlConnector\StructType\ChangeStoresRequestType;
20
use Etrias\PaazlConnector\StructType\ChangeStoresResponseType;
21
use Etrias\PaazlConnector\StructType\DeleteStoresRequest;
22
use Etrias\PaazlConnector\StructType\DeleteStoresResponse;
23
use Etrias\PaazlConnector\StructType\DeleteStoreType;
24
use Etrias\PaazlConnector\StructType\ListStoresRequest;
25
use Etrias\PaazlConnector\StructType\ListStoresResponse;
26
use Etrias\PaazlConnector\StructType\StoreDetailsType;
27
28
class StoresService
29
{
30
    use Processor;
31
32
    /**
33
     * @var SecurityServiceInterface
34
     */
35
    protected $securityService;
36
    /**
37
     * @var GeneralServiceType
38
     */
39
    protected $generalServiceType;
40
41
    /**
42
     * DocumentService constructor.
43
     *
44
     * @param GeneralServiceType       $generalServiceType
45
     * @param SecurityServiceInterface $securityService
46
     */
47
    public function __construct(GeneralServiceType $generalServiceType, SecurityServiceInterface $securityService)
48
    {
49
        $this->securityService = $securityService;
50
        $this->generalServiceType = $generalServiceType;
51
    }
52
53
    /**
54
     * @param StoreDetailsType[] $stores
55
     * @param null               $targetWebShop
56
     *
57
     * @return ChangeStoresResponseType
58
     */
59 View Code Duplication
    public function createStores(array $stores, $targetWebShop = null)
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...
60
    {
61
        $changeStores = [];
62
        foreach ($stores as $store) {
63
            $changeStore = new ChangeStoreDetailsType($this->securityService->getHash($store->getCode()));
64
            $changeStore->setName($store->getName())
65
                ->setAddress($store->getAddress())
66
                ->setBusinessHours($store->getBusinessHours())
67
                ->setCode($store->getCode())
68
                ->setCoordinates($store->getCoordinates());
69
            $changeStores[] = $changeStore;
70
        }
71
72
        $request = new ChangeStoresRequestType(
73
            $this->generalServiceType->getWebShopId(),
74
            $targetWebShop,
75
            $changeStores
76
        );
77
78
        $response = $this->generalServiceType->createStores($request);
79
80
        return $this->processResponse($response, $this->generalServiceType);
81
    }
82
83
    /**
84
     * @param StoreDetailsType[] $stores
85
     * @param null               $targetWebShop
86
     *
87
     * @return ChangeStoresResponseType
88
     */
89 View Code Duplication
    public function updateStores(array $stores, $targetWebShop = null)
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...
90
    {
91
        $changeStores = [];
92
        foreach ($stores as $store) {
93
            $changeStore = new ChangeStoreDetailsType($this->securityService->getHash($store->getCode()));
94
            $changeStore->setName($store->getName())
95
                ->setAddress($store->getAddress())
96
                ->setBusinessHours($store->getBusinessHours())
97
                ->setCode($store->getCode())
98
                ->setCoordinates($store->getCoordinates());
99
            $changeStores[] = $changeStore;
100
        }
101
102
        $request = new ChangeStoresRequestType(
103
            $this->generalServiceType->getWebShopId(),
104
            $targetWebShop,
105
            $changeStores
106
        );
107
108
        $response = $this->generalServiceType->updateStores($request);
109
110
        return $this->processResponse($response, $this->generalServiceType);
111
    }
112
113
    /**
114
     * @param array $storeCodes
115
     * @param null  $targetWebShop
116
     *
117
     * @return DeleteStoresResponse
118
     */
119 View Code Duplication
    public function deleteStores(array $storeCodes, $targetWebShop = null)
120
    {
121
        $stores = [];
122
123
        foreach ($storeCodes as $storeCode) {
124
            $stores[] = new DeleteStoreType($this->securityService->getHash($storeCode), $storeCode);
125
        }
126
127
        $request = new DeleteStoresRequest(
128
            $this->generalServiceType->getWebShopId(),
129
            $targetWebShop,
130
            $stores
131
        );
132
133
        $response = $this->generalServiceType->deleteStores($request);
134
135
        return $this->processResponse($response, $this->generalServiceType);
136
    }
137
138
    /**
139
     * @param null $targetWebShop
140
     *
141
     * @return ListStoresResponse
142
     */
143 View Code Duplication
    public function listStores($targetWebShop = null)
144
    {
145
        $today = new DateTime();
146
        $request = new ListStoresRequest(
147
            $this->securityService->getHash($today->format('Ymd')),
148
            $this->generalServiceType->getWebShopId(),
149
            $targetWebShop
150
        );
151
152
        $response = $this->generalServiceType->listStores($request);
153
154
        return $this->processResponse($response, $this->generalServiceType);
155
    }
156
}
157