Passed
Push — master ( 323542...b3d3d7 )
by Frank
06:59 queued 02:19
created

src/Rest/RestController.php (2 issues)

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;
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();
0 ignored issues
show
The method get_data() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        /** @scrutinizer ignore-call */ 
100
        $data = $response->get_data();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
101
        if (isset($data['id'])) {
102
            $data['id'] = $attachmentId;
103
            $response->set_data($data);
0 ignored issues
show
The method set_data() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
            $response->/** @scrutinizer ignore-call */ 
104
                       set_data($data);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
        }
105
106
        return $response;
107
    }
108
}
109