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

JobInvoiceAddressFactory::createService()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.439
cc 6
eloc 20
nc 12
nop 1
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
}