DocumentController::store()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 61
Code Lines 40

Duplication

Lines 9
Ratio 14.75 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 40
nc 4
nop 1
dl 9
loc 61
ccs 18
cts 18
cp 1
crap 3
rs 9.5147
c 0
b 0
f 0

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 Carbon\Carbon;
6
use Illuminate\Http\Request;
7
use Sausin\Signere\Document;
8
use Illuminate\Support\Facades\Config;
9
use Sausin\Signere\Http\Controllers\Controller;
10
11
class DocumentController extends Controller
12
{
13
    /** @var \Sausin\Signere\Document */
14
    protected $d;
15
16
    /**
17
     * Create a new controller instance.
18
     *
19
     * @param  \Sausin\Signere\Document $d
20
     */
21 5
    public function __construct(Document $d)
22
    {
23 5
        parent::__construct();
24
25 5
        $this->d = $d;
26 5
    }
27
28
    /**
29
     * Retrieves a list of documents corresponding to the jobId.
30
     *
31
     * @param  string $jobId
32
     * @return \Illuminate\Http\Response
33
     */
34 1
    public function index(string $jobId)
35
    {
36 1
        return $this->d->getList($jobId)
37 1
                ->getBody()
38 1
                ->getContents();
39
    }
40
41
    /**
42
     * Returns the url to sign the document for the given Signeeref
43
     * or the first Signeeref if not SigneerefId is specified.
44
     *
45
     * @param  string $documentId
46
     * @param  string $signeeRefId
47
     * @return \Illuminate\Http\Response
48
     */
49 1
    public function show(string $documentId, string $signeeRefId)
50
    {
51 1
        return $this->d->getSignUrl($documentId, $signeeRefId)
52 1
                ->getBody()
53 1
                ->getContents();
54
    }
55
56
    /**
57
     * Creates a new document to sign & returns
58
     * a document response object.
59
     *
60
     * @param  Request $request
61
     * @return \Illuminate\Http\Response
62
     */
63 1
    public function store(Request $request)
64
    {
65 1
        $this->validate($request, [
66 1
            'description' => 'required|string|min:1|max:255',
67
            'file_content' => 'required|string',
68
            'file_md5' => 'required|string|size:32',
69
            'filename' => 'required|string|min:1|max:255',
70
            'language' => 'required|string|in:EN,NO,SV,DA,FI',
71
            'sender_email' => 'sometimes|nullable|email',
72
            'sign_deadline' => 'sometimes|nullable|date',
73
            'job_id' => 'required|string|size:36',
74
            'title' => 'required|string|min:1|max:255',
75
            // unique ref is nothing but the signee ref returned
76
            // by signere when a receiver is created
77
            'signee_refs.*.unique_ref' => 'required|string|size:36',
78
            'signee_refs.*.first_name' => 'required|string|min:1|max:255',
79
            'signee_refs.*.last_name' => 'required|string|min:1|max:255',
80
            'signee_refs.*.email' => 'required|email|min:1|max:255',
81
        ]);
82
83
        // this is used to only set the keys which have been sent in
84
        $useKeys = [
85 1
            'ext_doc_id' => 'ExternalDocumentId',
86
            'description' => 'Description',
87
            'file_content' => 'FileContent',
88
            'file_md5' => 'FileMD5CheckSum',
89
            'filename' => 'FileName',
90
            'language' => 'Language',
91
            'sender_email' => 'SenderEmail',
92
            'sign_deadline' => 'SignDeadline',
93
            'job_id' => 'SignJobId',
94
            'title' => 'Title',
95
        ];
96
97
        // check which keys are available in the request
98 1
        $available = array_intersect(array_keys($useKeys), array_keys($request->all()));
99
100 1
        $body = [];
101
102
        // set the body up
103 1
        foreach ($available as $use) {
104 1
            $body[$useKeys[$use]] = $request->$use;
105
        }
106
107 1
        $body['SigneeRefs'] = [];
108
109
        // populate the signee references
110 1 View Code Duplication
        foreach ($request->signee_refs as $signee) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
111
            // append this to the body
112 1
            $body['SigneeRefs'][] = [
113 1
                'SigneeRefId' => $signee['unique_ref'],
114 1
                'FirstName' => $signee['first_name'],
115 1
                'LastName' => $signee['last_name'],
116 1
                'Email' => $signee['email'],
117
            ];
118
        }
119
120 1
        return $this->d->create($body)
121 1
                ->getBody()
122 1
                ->getContents();
123
    }
124
125
    /**
126
     * Changes the signature deadline for a given document.
127
     *
128
     * @param  Request $request
129
     * @param  string  $documentId
130
     * @return \Illuminate\Http\Response
131
     */
132 1
    public function update(Request $request, string $documentId)
133
    {
134 1
        $this->validate($request, [
135 1
            'new_deadline' => 'bail|required|date',
136
            'notify_email' => 'bail|nullable|boolean',
137
            'notify_sms' => 'bail|nullable|boolean',
138
        ]);
139
140 1
        $body = [];
141
142
        // setup the body
143 1
        $body['NewDeadline'] = $request->new_deadline;
144 1
        $body['NotifyEmail'] = $request->has('notify_email') ? $request->notify_email : false;
145 1
        $body['NotifySMS'] = $request->has('notify_sms') ? $request->notify_sms : false;
146 1
        $body['DocumentID'] = $documentId;
147 1
        $body['ProviderID'] = Config::get('signere.id');
148
149 1
        return $this->d->changeDeadline($body)
150 1
                ->getBody()
151 1
                ->getContents();
152
    }
153
154
    /**
155
     * Changes the signature deadline for a given document.
156
     *
157
     * @param  Request $request
158
     * @param  string  $documentId
159
     * @return \Illuminate\Http\Response
160
     */
161 1
    public function destroy(Request $request, string $documentId)
162
    {
163 1
        $this->validate($request, [
164 1
            'canceled_date' => 'bail|required|date',
165
            'explanation' => 'bail|nullable|string|max:255',
166
            'signature' => 'bail|nullable|string|max:150',
167
        ]);
168
169
        // this is used to only set the keys which have been sent in
170
        $useKeys = [
171 1
            'explanation' => 'Explanation',
172
            'signature' => 'Signature',
173
        ];
174
175
        // check which keys are available in the request
176 1
        $available = array_intersect(array_keys($useKeys), array_keys($request->all()));
177
178 1
        $body = [];
179
180
        // set the body up
181 1
        foreach ($available as $use) {
182 1
            $body[$useKeys[$use]] = $request->$use;
183
        }
184
185 1
        $body['CanceledDate'] = substr(
186 1
            Carbon::parse($request->canceled_date)->setTimezone('UTC')->toIso8601String(),
187 1
            0,
188 1
            19
189
        );
190 1
        $body['DocumentID'] = $documentId;
191
192 1
        return $this->d->cancel($body)
193 1
                ->getBody()
194 1
                ->getContents();
195
    }
196
}
197