UserSettingsFactory::createService()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace JhFlexiTime\Entity\Factory;
4
5
use JhFlexiTime\Entity\UserSettings;
6
use Zend\ServiceManager\FactoryInterface;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
9
/**
10
 * Class USerSettingsFactory
11
 * @package JhFlexiTime\Repository\Factory
12
 * @author Aydin Hassan <[email protected]>
13
 */
14
class UserSettingsFactory implements FactoryInterface
15
{
16
    /**
17
     * @param ServiceLocatorInterface $serviceLocator
18
     * @return \JhFlexiTime\Entity\UserSettings
19
     * @throws \InvalidArgumentException
20
     */
21
    public function createService(ServiceLocatorInterface $serviceLocator)
22
    {
23
        $userService = $serviceLocator->get('zfcuser_auth_service');
24
25
        if (!$userService->hasIdentity()) {
26
            throw new \InvalidArgumentException("User is not authenticated");
27
        }
28
29
        $userSettingsRepository = $serviceLocator->get('JhFlexiTime\Repository\UserSettingsRepository');
30
        $userSettings           = $userSettingsRepository->findOneByUser($userService->getIdentity());
31
32
        if (!$userSettings) {
33
            throw new \InvalidArgumentException("User does not have a settings row");
34
        }
35
36
        return $userSettings;
37
    }
38
}
39