DocumentProviderController::update()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 65
Code Lines 49

Duplication

Lines 65
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 49
c 1
b 0
f 0
nc 2
nop 1
dl 65
loc 65
ccs 11
cts 11
cp 1
crap 2
rs 9.3571

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sausin\Signere\Http\Controllers\Admin;
4
5
use Illuminate\Http\Request;
6
use Sausin\Signere\DocumentProvider;
7
use Illuminate\Support\Facades\Config;
8
use Sausin\Signere\Http\Controllers\Controller;
9
10
class DocumentProviderController extends Controller
11
{
12
    /** @var \Sausin\Signere\DocumentProvider */
13
    protected $dp;
14
15
    /**
16
     * Create a new controller instance.
17
     *
18
     * @param  \Sausin\Signere\DocumentProvider $dp
19
     */
20 5
    public function __construct(DocumentProvider $dp)
21
    {
22 5
        parent::__construct();
23
24 5
        $this->dp = $dp;
25 5
    }
26
27
    /**
28
     * Retrieves a document provider account.
29
     *
30
     * @return \Illuminate\Http\Response
31
     */
32 1
    public function index()
33
    {
34 1
        return $this->dp->getProviderAccount(Config::get('signere.id'))
35 1
                ->getBody()
36 1
                ->getContents();
37
    }
38
39
    /**
40
     * Retrieves usage for this account.
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44 1
    public function show()
45
    {
46 1
        return $this->dp->getUsage(Config::get('signere.id'))
47 1
                ->getBody()
48 1
                ->getContents();
49
    }
50
51
    /**
52
     * Creates a new document provider account
53
     * for submitting documents.
54
     *
55
     * @param  Request $request
56
     * @return \Illuminate\Http\Response
57
     */
58 2 View Code Duplication
    public function store(Request $request)
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...
59
    {
60 2
        $this->validate($request, [
61 2
            'address1' => 'bail|required|string|min:1|max:255',
62
            'address2' => 'bail|nullable|string|min:1|max:255',
63
            'city' => 'bail|required|string|min:1|max:255',
64
            'country' => 'bail|nullable|string|min:1|max:255',
65
            'billing_plan' => 'bail|nullable|string|in:Small,Medium,Large',
66
            'postcode' => [
67
                'bail',
68
                'required',
69
                'regex:/^\d{4}$/i',
70
            ],
71
            'company_email' => 'bail|required|email|max:255',
72
            'company_phone' => [
73
                'bail',
74
                'required',
75
                'string',
76
                'regex:/^\+47\d{8}$/i',
77
            ],
78
            'dealer_id' => 'bail|required|string|size:36',
79
            'legal_contact_email' => 'bail|required|email|max:255',
80
            'legal_contact_name' => 'bail|required|string|min:1|max:255',
81
            'legal_contact_phone' => [
82
                'bail',
83
                'required',
84
                'string',
85
                'regex:/^\+47\d{8}$/i',
86
            ],
87
            'mva_number' => 'bail|required|numeric|min:800000000|max:999999999',
88
            'name' => 'bail|required|string|min:1|max:255',
89
        ]);
90
91
        // this is used to only set the keys which have been sent in
92
        $useKeys = [
93 2
            'address1' => 'BillingAddress1',
94
            'address2' => 'BillingAddress2',
95
            'city' => 'BillingCity',
96
            'country' => 'BillingCountry',
97
            'billing_plan' => 'BillingPlan',
98
            'postcode' => 'BillingPostalCode',
99
            'company_email' => 'CompanyEmail',
100
            'company_phone' => 'CompanyPhone',
101
            'dealer_id' => 'DealerId',
102
            'legal_contact_email' => 'LegalContactEmail',
103
            'legal_contact_name' => 'LegalContactName',
104
            'legal_contact_phone' => 'LegalContactPhone',
105
            'mva_number' => 'MvaNumber',
106
            'name' => 'Name',
107
        ];
108
109
        // check which keys are available in the request
110 2
        $available = array_intersect(array_keys($useKeys), array_keys($request->all()));
111
112 2
        $body = [];
113
114
        // set the body up
115 2
        foreach ($available as $use) {
116 2
            $body[$useKeys[$use]] = $request->$use;
117
        }
118
119 2
        return $this->dp->create($body)
120 2
                ->getBody()
121 2
                ->getContents();
122
    }
123
124
    /**
125
     * Updates the information stored in a given
126
     * document provider account.
127
     *
128
     * @param  Request $request
129
     * @return \Illuminate\Http\Response
130
     */
131 1 View Code Duplication
    public function update(Request $request)
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...
132
    {
133 1
        $this->validate($request, [
134 1
            'address1' => 'bail|sometimes|string|min:1|max:255',
135
            'address2' => 'bail|sometimes|nullable|string|min:1|max:255',
136
            'city' => 'bail|sometimes|string|min:1|max:255',
137
            'country' => 'bail|sometimes|nullable|string|min:1|max:255',
138
            'billing_plan' => 'bail|sometimes|nullable|string|in:Small,Medium,Large',
139
            'postcode' => [
140
                'bail',
141
                'sometimes',
142
                'regex:/^\d{4}$/i',
143
            ],
144
            'company_email' => 'bail|sometimes|email|max:255',
145
            'company_phone' => [
146
                'bail',
147
                'sometimes',
148
                'string',
149
                'regex:/^\+47\d{8}$/i',
150
            ],
151
            'dealer_id' => 'bail|sometimes|string|size:36',
152
            'legal_contact_email' => 'bail|sometimes|email|max:255',
153
            'legal_contact_name' => 'bail|sometimes|string|min:1|max:255',
154
            'legal_contact_phone' => [
155
                'bail',
156
                'sometimes',
157
                'string',
158
                'regex:/^\+47\d{8}$/i',
159
            ],
160
            'mva_number' => 'bail|sometimes|numeric|min:800000000|max:999999999',
161
            'name' => 'bail|sometimes|string|min:1|max:255',
162
        ]);
163
164
        // this is used to only set the keys which have been sent in
165
        $useKeys = [
166 1
            'address1' => 'BillingAddress1',
167
            'address2' => 'BillingAddress2',
168
            'city' => 'BillingCity',
169
            'country' => 'BillingCountry',
170
            'billing_plan' => 'BillingPlan',
171
            'postcode' => 'BillingPostalCode',
172
            'company_email' => 'CompanyEmail',
173
            'company_phone' => 'CompanyPhone',
174
            'dealer_id' => 'DealerId',
175
            'legal_contact_email' => 'LegalContactEmail',
176
            'legal_contact_name' => 'LegalContactName',
177
            'legal_contact_phone' => 'LegalContactPhone',
178
            'mva_number' => 'MvaNumber',
179
            'name' => 'Name',
180
        ];
181
182
        // check which keys are available in the request
183 1
        $available = array_intersect(array_keys($useKeys), array_keys($request->all()));
184
185 1
        $body = [];
186
187
        // set the body up
188 1
        foreach ($available as $use) {
189 1
            $body[$useKeys[$use]] = $request->$use;
190
        }
191
192 1
        return $this->dp->update($body)
193 1
                ->getBody()
194 1
                ->getContents();
195
    }
196
}
197