Passed
Pull Request — master (#631)
by ANTHONIUS
08:16
created

Settings::getUserRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Settings\Repository;
4
5
use Auth\Entity\UserInterface;
6
use Core\Entity\IdentifiableEntityInterface;
7
use Doctrine\ODM\MongoDB\LockMode;
8
use Interop\Container\ContainerInterface;
9
use Laminas\ServiceManager\ServiceLocatorInterface;
10
use Laminas\Mvc\MvcEvent;
11
use Settings\Entity\SettingsContainer as SettingsEntity;
12
use Core\Repository\AbstractRepository;
13
14
class Settings extends AbstractRepository
15
{
16
    protected $userRepository;
17
    protected $settingsByUser;
18
    protected $container;
19
    
20
    public function __construct()
21
    {
22
        $this->settingsByUser = array();
23
    }
24
    
25
    public function setContainer(ContainerInterface $container)
26
    {
27
        $this->container = $container;
28
    }
29
    
30
    protected function getContainer()
31
    {
32
        return $this->container;
33
    }
34
    
35
    public function setUserRepository($userRepository)
36
    {
37
        $this->userRepository = $userRepository;
38
    }
39
40
    protected function getUserRepository()
41
    {
42
        return $this->userRepository;
43
    }
44
45
    /**
46
     * the $id for an entity 'setting' is the same as for the entity 'user'
47
     * @param UserInterface $user
48
     * @return mixed|SettingsEntity|null
49
     */
50
    public function getSettingsByUser($user)
51
    {
52
        $userId = $user;
53
        $userEntity = null;
54
        if ($user instanceof IdentifiableEntityInterface) {
0 ignored issues
show
introduced by
$user is always a sub-type of Core\Entity\IdentifiableEntityInterface.
Loading history...
55
            $userId = $user->getId();
56
            $userEntity = $user;
57
        }
58
        if (isset($this->settingsByUser[$userId])) {
59
            return $this->settingsByUser[$user];
60
        }
61
        if (empty($userEntity)) {
62
            $UserRepository = $this->getUserRepository();
63
            $userEntity = $UserRepository->find($userId);
64
        }
65
        
66
        if (isset($userEntity)) {
67
            $settingsData = $userEntity->getSettings();
68
            $this->settingsByUser[$userId] = new SettingsEntity();
69
            $this->settingsByUser[$userId]->setData($settingsData)->spawnAsEntities();
70
            return $this->settingsByUser[$userId];
71
        }
72
        return null;
73
    }
74
    
75
    public function onPostDispatch(MvcEvent $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed. ( Ignorable by Annotation )

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

75
    public function onPostDispatch(/** @scrutinizer ignore-unused */ MvcEvent $e)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77
        //throw new \Exception("Test");
78
        $UserRepository = $this->getUserRepository();
79
        foreach ($this->settingsByUser as $user => $settings) {
80
            if (!empty($user) && $settings->hasChanged()) {
81
                $userEntity = $UserRepository->find($user);
82
                $a = $settings->toArray();
83
                $userEntity->setSettings($a);
84
                $id = $UserRepository->save($userEntity);
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
85
            }
86
        }
87
    }
88
    
89
    public function find($user, $lockMode = LockMode::NONE, ?int $lockVersion = null): ?object
90
    {
91
        return $this->getSettingsByUser($user);
92
    }
93
    
94
    public function getEntityByStrategy($namespace)
95
    {
96
        $configAccess = $this->getContainer()->get('ConfigAccess');
97
        $settings = $configAccess->getByKey('settings');
98
        if (array_key_exists($namespace, $settings) && array_key_exists('entity', $settings[$namespace])) {
99
            $entity = new $settings[$namespace]['entity'];
100
            $entity->setConfig($settings[$namespace]);
101
            return $entity;
102
        }
103
        return null;
104
    }
105
    
106
    public function getFormular($formular = null)
107
    {
108
        $form = null;
109
        if (isset($formular) && is_string($formular)) {
110
            $formElementManager = $this->getContainer()->get('FormElementManager');
111
            if ($formElementManager->has($formular)) {
112
                $form = $formElementManager->get($formular);
113
            }
114
            if (!isset($form)) {
115
                if ($this->getContainer()->has($formular)) {
116
                    $form = $this->getContainer()->get($formular);
117
                }
118
            }
119
        }
120
        return $form;
121
    }
122
}
123