Test Failed
Push — master ( 21482a...25bd35 )
by Marcel
03:26
created

SaasClientService::getCurrentClient()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
c 1
b 1
f 0
dl 0
loc 16
rs 10
cc 3
nc 3
nop 0
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\Session\SessionInterface;
17
18
class SaasClientService
19
{
20
    private const SaasClientSessionIndex = 'SAASCLIENT';
21
    /** @var EntityManagerInterface */
22
    private $em;
23
24
    /** @var SessionInterface */
25
    private $session;
26
27
    private $clientEntity;
28
29
    public function __construct(ContainerInterface $container, EntityManagerInterface $em, SessionInterface $session)
30
    {
31
        $this->em = $em;
32
        $this->session = $session;
33
        $this->clientEntity = $container->getParameter('fluxter.saasprovider.cliententity');
34
    }
35
36
    public function __call($name, $arguments)
37
    {
38
        $client = $this->getCurrentClient();
39
40
        $method = "get$name";
41
        if (method_exists($client, $method)) {
42
            return $client->$method();
43
        }
44
        $method = "is$name";
45
        if (method_exists($client, $method)) {
46
            return $client->$method();
47
        }
48
49
        throw new \Exception("Unknown SaaS-Client function / variable: {$name}!");
50
    }
51
52
    public function createClient(array $parameters)
53
    {
54
        /** @var SaasClientInterface $client */
55
        $client = new $this->clientEntity();
56
57
        /** @var SaasParameterInterface $parameter */
58
        foreach ($parameters as $parameter) {
59
            $client->addParameter($parameter);
60
        }
61
62
        $this->em->persist($client);
63
        $this->em->flush($client);
64
65
        return $client;
66
    }
67
68
    public function getCurrentClient()
69
    {
70
        if (!$this->session->has(self::SaasClientSessionIndex)) {
71
            throw new \Exception('SAAS-CLIENT SESSION VARIABLE NOT SPECIFIED');
72
        }
73
74
        // Todo Entity name from configuration
75
        $repo = $this->em->getRepository($this->clientEntity);
76
77
        /** @var SaasClientInterface $client */
78
        $client = $repo->findOneById($this->session->get(self::SaasClientSessionIndex));
0 ignored issues
show
Bug introduced by
The method findOneById() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findOneBy()? ( Ignorable by Annotation )

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

78
        /** @scrutinizer ignore-call */ 
79
        $client = $repo->findOneById($this->session->get(self::SaasClientSessionIndex));

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...
79
        if (null == $client) {
80
            throw new \Exception('SAAS-CLIENT NOT FOUND!');
81
        }
82
83
        return $client;
84
    }
85
}
86