Passed
Pull Request — master (#100)
by Guido
05:27
created

RestController::get_item()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 21
rs 9.8666
cc 3
nc 3
nop 1
1
<?php # -*- coding: utf-8 -*-
2
declare(strict_types=1);
3
4
namespace MultisiteGlobalMedia\Rest;
5
6
use MultisiteGlobalMedia\Helper;
7
use MultisiteGlobalMedia\SingleSwitcher;
8
use MultisiteGlobalMedia\Site;
9
use MultisiteGlobalMedia\SiteSwitcher;
10
use WP_REST_Attachments_Controller;
0 ignored issues
show
Bug introduced by
The type WP_REST_Attachments_Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Class RestController
14
 *
15
 * Disable our codestyle for several topics, because we extend a WP core class, method.
16
 */
17
class RestController extends WP_REST_Attachments_Controller
18
{
19
    use Helper;
20
21
    /**
22
     * @var Site
23
     */
24
    private $site;
25
26
    /**
27
     * @var SiteSwitcher
28
     */
29
    private $siteSwitcher;
30
31
    /**
32
     * {@inheritDoc}
33
     *
34
     * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
35
     * phpcs:disable Inpsyde.CodeQuality.VariablesName.SnakeCaseVar
36
     */
37
    public function __construct($post_type)
38
    {
39
        // phpcs:enable
40
        $this->site = new Site();
41
        $this->siteSwitcher = new SingleSwitcher();
42
43
        // phpcs:ignore Inpsyde.CodeQuality.VariablesName.SnakeCaseVar
44
        parent::__construct($post_type);
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * phpcs:disable Inpsyde.CodeQuality.NoAccessors.NoGetter
51
     * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
52
     * phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
53
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
54
     */
55
    public function get_item_permissions_check($request)
56
    {
57
        // phpcs:enable
58
        $idPrefix = $this->site->idSitePrefix();
59
60
        if (!$this->idPrefixIncludedInAttachmentId((int)$request['id'], $idPrefix)) {
61
            return parent::get_item_permissions_check($request);
62
        }
63
64
        // Clone so the original id is available in other methods.
65
        $requestClone = clone $request;
66
        $requestClone['id'] = $this->stripSiteIdPrefixFromAttachmentId(
67
            $idPrefix,
68
            (int)$request['id']
69
        );
70
71
        $this->siteSwitcher->switchToBlog($this->site->id());
72
        $response = parent::get_item_permissions_check($requestClone);
73
        $this->siteSwitcher->restoreBlog();
74
75
        return $response;
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     *
81
     * phpcs:disable Inpsyde.CodeQuality.NoAccessors.NoGetter
82
     * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
83
     * phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
84
     * phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
85
     */
86
    public function get_item($request)
87
    {
88
        // phpcs:enable
89
        $idPrefix = $this->site->idSitePrefix();
90
91
        if (!$this->idPrefixIncludedInAttachmentId((int)$request['id'], $idPrefix)) {
92
            return parent::get_item($request);
93
        }
94
95
        $attachmentId = (int)$request['id'];
96
        $request['id'] = $this->stripSiteIdPrefixFromAttachmentId($idPrefix, $attachmentId);
97
        $this->siteSwitcher->switchToBlog($this->site->id());
98
        $response = parent::get_item($request);
99
        $data = $response->get_data();
100
101
        if (isset($data['id'])) {
102
            $data['id'] = $attachmentId;
103
            $response->set_data($data);
104
        }
105
106
        return $response;
107
    }
108
}
109