Completed
Push — develop ( d65f66...535cf4 )
by
unknown
28:52 queued 13:57
created

GetOrganizationHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 88
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 4 1
C process() 0 51 12
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013-2016 Cross Solution (http://cross-solution.de)
7
 * @author cbleek
8
 * @license   MIT
9
 */
10
11
/** */
12
namespace Organizations\Controller\Plugin;
13
14
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
15
use Core\Repository\RepositoryService;
16
use Auth\AuthenticationService;
17
use Auth\Exception\UnauthorizedAccessException;
18
use Zend\Mvc\Controller\Plugin\Params;
19
use Acl\Controller\Plugin\Acl;
20
21
/**
22
 * Class GetOrganizationHandler
23
 *
24
 * @package Organization\Controller\Plugin
25
 */
26
class GetOrganizationHandler extends AbstractPlugin {
27
28
    /**
29
     * @var RepositoryService
30
     */
31
    protected $repositoryService;
32
33
    /**
34
     * @var AuthenticationService
35
     */
36
    protected $auth;
37
38
    /**
39
     * @var \Acl\Controller\Plugin\Acl
40
     */
41
    protected $acl;
42
43
    public function __construct(RepositoryService $repositoryService,AuthenticationService $auth, Acl $acl) {
44
        $this->repositoryService=$repositoryService;
45
        $this->auth=$auth;
46
        $this->acl=$acl;
47
    }
48
49
    public function __invoke()
50
    {
51
        return $this;
52
    }
53
54
    /**
55
     * @param Params $params
56
     * @param bool   $allowDraft
57
     *
58
     * @return object|\Organizations\Entity\Organization
59
     * @throws UnauthorizedAccessException
60
     * @throws \Doctrine\ODM\MongoDB\LockException
61
     */
62
    public function process(Params $params,$allowDraft = true)
63
    {
64
        $repositories   = $this->repositoryService;
65
        $organizationRepository = $this->repositoryService->get('Organizations/Organization');
66
67
        $idFromRoute = $params('id', 0);
68
        $idFromSubForm = $params()->fromPost('id', 0);
69
        $user = $this->auth->getUser(); /* @var $user \Auth\Entity\UserInterface */
70
71
        /* @var $organizationId string */
72
        $organizationId = empty($idFromRoute)?$idFromSubForm:$idFromRoute;
73
74
        $editOwnOrganization = '__my__' === $organizationId;
75
76
        if ($editOwnOrganization) {
77
            /* @var $userOrg \Organizations\Entity\OrganizationReference */
78
            $userOrg = $user->getOrganization();
79
            if ($userOrg->hasAssociation() && !$userOrg->isOwner()) {
80
                throw new UnauthorizedAccessException('You may not edit this organization as you are only employer.');
81
            }
82
            $organizationId = $userOrg->hasAssociation() ? $userOrg->getId() : 0;
83
        }
84
85
        if (empty($organizationId) && $allowDraft) {
86
            /* @var $organization \Organizations\Entity\Organization */
87
            $organization = $organizationRepository->findDraft($user);
88
            if (empty($organization)) {
89
                $organization = $organizationRepository->create();
0 ignored issues
show
Bug introduced by
The method create() does not exist on Doctrine\ODM\MongoDB\DocumentRepository. Did you maybe mean createQueryBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
                $organization->setIsDraft(true);
91
                $organization->setUser($user);
92
                if (!$editOwnOrganization) {
93
                    /* @var $parent \Organizations\Entity\OrganizationReference */
94
                    $parent = $user->getOrganization();
95
                    if (!$parent->hasAssociation()) {
96
                        throw new \RuntimeException('You cannot create organizations, because you do not belong to a parent organization. Use "User menu -> create my organization" first.');
97
                    }
98
                    $organization->setParent($parent->getOrganization());
99
                }
100
101
                $repositories->store($organization);
102
103
            }
104
            return $organization;
105
        }
106
107
        $organization      = $organizationRepository->find($organizationId);
108
        if (!$organization) {
109
            throw new \RuntimeException('No Organization found with id "' . $organizationId . '"');
110
        }
111
        return $organization;
112
    }
113
}