Completed
Push — develop ( 168e99...0122be )
by
unknown
18:37 queued 07:11
created

JobInvoiceAddressFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 4
dl 0
loc 40
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B createService() 0 28 6
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Orders\Factory\Entity;
12
13
use Core\Entity\Hydrator\EntityHydrator;
14
use Orders\Entity\InvoiceAddress;
15
use Settings\Entity\Hydrator\SettingsEntityHydrator;
16
use Zend\ServiceManager\FactoryInterface;
17
use Zend\ServiceManager\ServiceLocatorInterface;
18
19
/**
20
 * ${CARET}
21
 * 
22
 * @author Mathias Gelhausen <[email protected]>
23
 * @todo write test 
24
 */
25
class JobInvoiceAddressFactory implements FactoryInterface
26
{
27
    /**
28
     * Create service
29
     *
30
     * @param ServiceLocatorInterface $serviceLocator
31
     *
32
     * @return mixed
33
     */
34
    public function createService(ServiceLocatorInterface $serviceLocator)
35
    {
36
        $auth = $serviceLocator->get('AuthenticationService');
37
        $user = $auth->getUser();
38
        $settings = $user->getSettings('Orders');
39
        $invoiceAddress = $settings->getInvoiceAddress();
40
        if (!$invoiceAddress->get('name')) {
41
            $invoiceAddress = false;
42
            $org = $user->getOrganization();
43
            if ($org->isEmployee()) {
44
                $orgUser = $org->isHiringOrganization() ? $org->getParent()->getUser() : $org->getUser();
45
                $invoiceAddress = $orgUser->getSettings('Orders')->getInvoiceAddress();
46
                if (!$invoiceAddress->get('name')) {
47
                    $invoiceAddress = false;
48
                }
49
            }
50
        }
51
52
        $entity = new InvoiceAddress();
53
54
        if ($invoiceAddress) {
55
            $entityHydrator = new EntityHydrator();
56
            $settingsHydrator = new SettingsEntityHydrator();
57
            $data     = $settingsHydrator->extract($invoiceAddress);
58
            $entity   = $entityHydrator->hydrate($data, $entity);
59
        }
60
        return $entity;
61
    }
62
63
64
}