DeleteResourceQuery::execute()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5.0031

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95
rs 8.439
cc 5
eloc 19
nc 5
nop 1
crap 5.0031
1
<?php
2
3
namespace Dekalee\Cdn77\Query;
4
5
use Dekalee\Cdn77\Exception\QueryErrorException;
6
7
/**
8
 * Class DeleteResourceQuery
9
 */
10
class DeleteResourceQuery extends AbstractResourcesAwareQuery implements QueryInterface
11
{
12
    const URL = 'https://api.cdn77.com/v2.0/cdn-resource/delete';
13
14
    /**
15
     * @param null $resource
16
     *
17
     * @return mixed|null
18
     * @throws QueryErrorException
19
     */
20 3
    public function execute($resource = null)
21
    {
22 3
        if (null === $resource) {
23 1
            throw new QueryErrorException('Could not delete no resource');
24
        }
25
26 2
        $this->populateCDNResources();
27
28 2
        $cdnResources = array_filter($this->cdnResources, function($value, $key) use ($resource) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29 2
            return $value['cname'] == $resource;
30 2
        }, ARRAY_FILTER_USE_BOTH);
31
32 2
        if (count($cdnResources) > 1) {
33 1
            throw new QueryErrorException('Could not delete more than one resource');
34
        }
35
36 1
        foreach ($cdnResources as $cdnResource) {
37 1
            $response = $this->client->post($this->url, [
38
                'form_params' => [
39 1
                    'id' => $cdnResource['id'],
40 1
                    'login' => $this->login,
41 1
                    'passwd' => $this->password,
42
                ]
43 1
            ]);
44 1
            $data = json_decode($response->getBody()->getContents(), true);
45
46 1
            if ('ok' !== $data['status']) {
47
                throw new QueryErrorException(json_encode($data));
48
            }
49 1
        }
50
51 1
        return;
52
    }
53
}
54