Completed
Pull Request — master (#333)
by Simon
04:01
created

removePluginAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
use Shopware\Bundle\PluginInstallerBundle\Service\InstallerService;
9
use Shopware\Components\CSRFWhitelistAware;
10
use ShopwarePlugins\Connect\Components\Config;
11
use ShopwarePlugins\Connect\Components\Logger;
12
13
/**
14
 * @category  Shopware
15
 * @package   Shopware\Plugins\SwagConnect
16
 * @copyright Copyright (c) 2013, shopware AG (http://www.shopware.de)
17
 * @author    Heiner Lohaus
18
 */
19
class Shopware_Controllers_Backend_ConnectGateway extends \Enlight_Controller_Action implements CSRFWhitelistAware
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
20
{
21
    /**
22
     * @var Config
23
     */
24
    private $configComponent;
25
26
    /**
27
     * Disable authentication and JSon renderer
28
     */
29
    public function init()
30
    {
31
        Shopware()->Plugins()->Backend()->Auth()->setNoAuth();
32
        Shopware()->Plugins()->Controller()->ViewRenderer()->setNoRender();
33
    }
34
35
    /**
36
     * @return Logger
37
     */
38
    public function getLogger()
39
    {
40
        $loggingEnabled = $this->getConfigComponent()->getConfig('logRequest');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $loggingEnabled is correct as $this->getConfigComponen...getConfig('logRequest') (which targets ShopwarePlugins\Connect\...nts\Config::getConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
41
42
        return new Logger(Shopware()->Db(), $loggingEnabled);
0 ignored issues
show
Documentation introduced by
$loggingEnabled is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
    }
44
45
    /**
46
     * @return \Shopware\Connect\SDK
47
     */
48
    public function getSDK()
49
    {
50
        return Shopware()->Container()->get('ConnectSDK');
51
    }
52
53
    public function getRestApiRequest()
54
    {
55
        return Shopware()->Container()->get('swagconnect.rest_api_request');
56
    }
57
58
    /**
59
     * @return InstallerService
60
     */
61
    public function getPluginManager()
62
    {
63
        return Shopware()->Container()->get('shopware_plugininstaller.plugin_manager');
64
    }
65
66
    /**
67
     * @return Config
68
     */
69
    public function getConfigComponent()
70
    {
71
        if (!$this->configComponent) {
72
            $this->configComponent = new Config(Shopware()->Models());
73
        }
74
75
        return $this->configComponent;
76
    }
77
78
    /**
79
     * Main connect interface
80
     *
81
     * @throws \Exception
82
     */
83
    public function indexAction()
0 ignored issues
show
Coding Style introduced by
indexAction uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
    {
85
        $this->Response()->setHeader('Content-Type', 'text/xml; charset=utf-8');
86
87
        $request = file_get_contents('php://input');
88
        $logger = $this->getLogger();
89
90
        try {
91
            $this->get('events')->notify(
92
                'Shopware_Connect_SDK_Handle_Before',
93
                [ $request ]
94
            );
95
96
            $sdk = $this->getSDK();
97
            $result = $sdk->handle(
98
                $request
99
            );
100
101
            $this->get('events')->notify(
102
                'Shopware_Connect_SDK_Handle_After',
103
                [ $request, $result ]
104
            );
105
        } catch (\Exception $e) {
106
            // Always write errors to the log
107
            $logger->write(true, $request, $e);
108
            $logger->write(true, 'Headers: ' . print_r($_SERVER, true), $e, 'request-headers');
109
            throw $e;
110
        }
111
112
        $logger->write(false, $request, $result);
113
114
        echo $result;
115
    }
116
117
    public function removePluginAction()
118
    {
119
        $this->Response()->setHeader('Content-Type', 'application/json; charset=utf-8');
120
121
        $request = file_get_contents('php://input');
122
123
        $apiRequest = $this->getRestApiRequest();
124
        $result = $apiRequest->verifyRequest($request);
125
126
        if ($result->isOk()) {
127
            /** @var InstallerService $pluginManager */
128
            $pluginManager = $this->getPluginManager();
129
            $plugin = $pluginManager->getPluginByName('SwagConnect');
130
            $pluginManager->uninstallPlugin($plugin);
131
        }
132
133
        echo $result->getContent();
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function getWhitelistedCSRFActions()
140
    {
141
        return ['index', 'removePlugin'];
142
    }
143
}
144