Passed
Push — master ( 9fafbe...a40188 )
by Marcel
07:47
created

SaasClientService   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 4
Bugs 2 Features 1
Metric Value
wmc 18
eloc 53
dl 0
loc 128
rs 10
c 4
b 2
f 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createClient() 0 14 2
A getCurrentClient() 0 27 6
A saveSaasClientSession() 0 2 1
A getCurrentHttpHost() 0 9 2
A __construct() 0 6 1
A __call() 0 14 3
A discoverClient() 0 11 2
A resetSaasClientSession() 0 2 1
1
<?php
2
3
/*
4
 * This file is part of the SaasProviderBundle package.
5
 * (c) Fluxter <http://fluxter.net/>
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Fluxter\SaasProviderBundle\Service;
11
12
use Doctrine\ORM\EntityManagerInterface;
13
use Fluxter\SaasProviderBundle\Model\SaasClientInterface;
14
use Fluxter\SaasProviderBundle\Model\SaasParameterInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Symfony\Component\HttpFoundation\Session\SessionInterface;
18
19
class SaasClientService
20
{
21
    private const SaasClientSessionIndex = 'SAASCLIENT';
22
23
    /** @var EntityManagerInterface */
24
    private $em;
25
26
    /** @var SessionInterface */
27
    private $session;
28
29
    /** @var string */
30
    private $saasClientEntity;
31
32
    /** @var RequestStack */
33
    private $requestStack;
34
35
    public function __construct(ContainerInterface $container, EntityManagerInterface $em, SessionInterface $session, RequestStack $requestStack)
36
    {
37
        $this->em = $em;
38
        $this->session = $session;
39
        $this->saasClientEntity = $container->getParameter('saas_provider.client_entity');
40
        $this->requestStack = $requestStack;
41
    }
42
43
    public function __call($name, $arguments)
44
    {
45
        $client = $this->getCurrentClient();
46
47
        $method = "get$name";
48
        if (method_exists($client, $method)) {
49
            return $client->$method();
50
        }
51
        $method = "is$name";
52
        if (method_exists($client, $method)) {
53
            return $client->$method();
54
        }
55
56
        throw new \Exception("Unknown SaaS-Client function / variable: {$name}!");
57
    }
58
59
    public function createClient(array $parameters)
60
    {
61
        /** @var SaasClientInterface $client */
62
        $client = new $this->saasClientEntity();
63
64
        /** @var SaasParameterInterface $parameter */
65
        foreach ($parameters as $parameter) {
66
            $client->addParameter($parameter);
67
        }
68
69
        $this->em->persist($client);
70
        $this->em->flush($client);
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\Persistence\ObjectManager::flush() has too many arguments starting with $client. ( Ignorable by Annotation )

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

70
        $this->em->/** @scrutinizer ignore-call */ 
71
                   flush($client);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
71
72
        return $client;
73
    }
74
75
    /**
76
     * Returns the current http host in lower case.
77
     * For example: "http://localhost:8000" returns "localhost"
78
     * https://test.test.de would return "test.test.de"
79
     *
80
     * @return string
81
     */
82
    private function getCurrentHttpHost() : string
83
    {
84
        $url = $this->requestStack->getCurrentRequest()->getHttpHost();
85
        $url = strtolower($url);
86
        if (strpos($url, ':') !== false) {
87
            $url = preg_replace('/:(.*)/', '', $url);
88
        }
89
90
        return strtolower($url);
91
    }
92
93
    private function discoverClient() : ?SaasClientInterface
94
    {
95
        $url = $this->getCurrentHttpHost();
96
        $repo = $this->em->getRepository($this->saasClientEntity);
97
        $client = $repo->findOneBy(['url' => $url]);
98
        if ($client == null) {
99
            return null;
100
        }
101
        
102
        $this->saveSaasClientSession($client);
103
        return $client;
104
    }
105
106
    private function resetSaasClientSession() {
107
        $this->session->set(self::SaasClientSessionIndex, null);
108
    }
109
110
    private function saveSaasClientSession(SaasClientInterface $client) {
111
        $this->session->set(self::SaasClientSessionIndex, $client->getId());
112
    }
113
114
    /**
115
     * Returns the current client, recognized by the url and the client entity
116
     *
117
     * @param boolean $autodiscover
118
     * @return SaasClientInterface|null
119
     */
120
    public function getCurrentClient(bool $autodiscover = true) : ?SaasClientInterface
121
    {
122
        if (!$this->session->has(self::SaasClientSessionIndex) || $this->session->get(self::SaasClientSessionIndex) == null) {
123
            if ($autodiscover) {
124
                $this->discoverClient();
125
                return $this->getCurrentClient(false);
126
            }
127
128
            return null;
129
        }
130
131
        $repo = $this->em->getRepository($this->saasClientEntity);
132
        /** @var SaasClientInterface $client */
133
        $client = $repo->find($this->session->get(self::SaasClientSessionIndex));
134
        if (null == $client) {
135
            return null;
136
        }
137
        
138
        // Validate
139
        $url = $this->getCurrentHttpHost();
140
        if (strtolower($client->getUrl()) != strtolower($url)) {
141
            $this->resetSaasClientSession();
142
            $this->discoverClient();
143
            return $this->getCurrentClient(false);
144
        }
145
146
        return $client;
147
    }
148
}
149