Completed
Push — develop ( d65f66...535cf4 )
by
unknown
13:48
created

GetOrganizationHandler::process()   C

Complexity

Conditions 12
Paths 38

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
rs 5.6668
cc 12
eloc 30
nc 38
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}