Completed
Push — develop ( 8761a6...ffee1d )
by
unknown
06:50
created

JobInvoiceAddressFactory::createService()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 47
rs 5.7377
cc 8
eloc 34
nc 36
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->getCompany()) {
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->getCompany()) {
47
                    $invoiceAddress = false;
48
                }
49
            }
50
        }
51
52
        /* Still no invoiceAddress? Let's create one from Company data. */
53
        if (!$invoiceAddress) {
54
            $org = $user->getOrganization();
55
            if ($org->isHiringOrganization()) { $org = $org->getParent(); }
56
            /* @var \Auth\Entity\Info $orgUserInfo */
57
            $orgContact = $org->getContact();
58
            $orgUserInfo = $org->getUser()->getInfo();
59
60
            $invoiceAddress = new InvoiceAddress();
61
            $invoiceAddress
62
                ->setGender($orgUserInfo->getGender())
63
                ->setName($orgUserInfo->getDisplayName(false))
64
                ->setCompany($org->getOrganizationName()->getName())
65
                ->setStreet($orgContact->getStreet() . ' ' . $orgContact->getHouseNumber())
66
                ->setZipCode($orgContact->getPostalcode())
67
                ->setCity($orgContact->getCity())
68
                ->setEmail($orgUserInfo->getEmail());
69
        }
70
71
        $entity = new InvoiceAddress();
72
73
        if ($invoiceAddress) {
74
            $entityHydrator = new EntityHydrator();
75
            $settingsHydrator = new SettingsEntityHydrator();
76
            $data     = $settingsHydrator->extract($invoiceAddress);
77
            $entity   = $entityHydrator->hydrate($data, $entity);
78
        }
79
        return $entity;
80
    }
81
82
83
}