Completed
Pull Request — master (#420)
by Tobias
03:09
created

RemoteShopService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 107
rs 10
c 1
b 0
f 0
wmc 12
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isPingRemoteShopSuccessful() 0 8 2
A tryPingRemoteShop() 0 8 2
A handlePingRemoteShopError() 0 8 2
A isExceptionFatal() 0 8 2
A getSnHttpClient() 0 8 1
A sendAddToBaskedFailedNotificationToSocialNetwork() 0 13 2
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
namespace ShopwarePlugins\Connect\Services;
9
10
use SebastianBergmann\GlobalState\RuntimeException;
11
use Shopware\Connect\SDK;
12
use ShopwarePlugins\Connect\Components\ConfigFactory;
13
use ShopwarePlugins\Connect\Components\SnHttpClient;
14
15
/**
16
 * Class RemoteShopService
17
 * @package ShopwarePlugins\Connect\Services
18
 */
19
class RemoteShopService
20
{
21
    /**
22
     * @var SDK
23
     */
24
    private $sdk;
25
26
    /**
27
     * RemoteShopService constructor.
28
     * @param SDK $sdk
29
     */
30
    public function __construct(SDK $sdk)
31
    {
32
        $this->sdk = $sdk;
33
    }
34
35
    /**
36
     * @param $shopId
37
     * @return bool
38
     */
39
    public function isPingRemoteShopSuccessful($shopId)
40
    {
41
        try {
42
            return $this->tryPingRemoteShop($shopId);
43
        } catch (\RuntimeException $exception) {
44
            return $this->handlePingRemoteShopError($shopId, $exception);
45
        }
46
    }
47
48
    /**
49
     * @param $shopId
50
     * @return bool
51
     */
52
    private function tryPingRemoteShop($shopId)
53
    {
54
        if ($this->sdk->pingShop($shopId) !== 'pong') {
0 ignored issues
show
Bug introduced by
The method pingShop() does not seem to exist on object<Shopware\Connect\SDK>.

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...
55
            throw new RuntimeException("Ping was not returning excepted result 'pong'.");
56
        } else {
57
            return true;
58
        }
59
    }
60
61
    /**
62
     * @param $shopId
63
     * @param $exception
64
     * @return bool
65
     */
66
    private function handlePingRemoteShopError($shopId, $exception)
67
    {
68
        if ($this->isExceptionFatal($exception)) {
69
            $this->sendAddToBaskedFailedNotificationToSocialNetwork($shopId);
70
            return false;
71
        }
72
        return true;
73
    }
74
75
    /**
76
     * Checks if the exception is fatal by looking at the exceptions
77
     * message. The exception is not fatal when the ping service is
78
     * just not available yet. Receiving this exception from SDK would
79
     * actually be a successful ping of the remote shop. This is needed
80
     * because of backwards compatibility with older SDK versions less
81
     * than v2.0.8
82
     *
83
     * @param $exception
84
     * @return bool
85
     */
86
    public function isExceptionFatal($exception)
87
    {
88
        if (strpos($exception->getMessage(),
89
            "Uncaught Shopware\Connect\SecurityException: No Authorization to call service 'ping'.") !== false) {
90
            return false;
91
        }
92
        return true;
93
    }
94
95
    /**
96
     * @return SnHttpClient
97
     * @throws \Exception
98
     * @todo: refactor when using 5.2 plugin base.
99
     */
100
    private function getSnHttpClient()
101
    {
102
        return new SnHttpClient(
103
            Shopware()->Container()->get('http_client'),
104
            new \Shopware\Connect\Gateway\PDO(Shopware()->Db()->getConnection()),
105
            ConfigFactory::getConfigInstance()
106
        );
107
    }
108
109
    /**
110
     * @param $shopId
111
     */
112
    public function sendAddToBaskedFailedNotificationToSocialNetwork($shopId)
113
    {
114
        try {
115
            $this->getSnHttpClient()->sendRequestToConnect(
116
                'account/add-to-basket-failed',
117
                [
118
                    'supplierShopId' => $shopId
119
                ]
120
            );
121
        } catch (\Exception $exception) {
122
            Shopware()->PluginLogger()->error('Unable to send request to SocialNetwork: ' . $exception);
123
        }
124
    }
125
}
126