Completed
Push — develop ( 09456b...2094a2 )
by Mathias
14s queued 10s
created

ProfileController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 136
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
B indexAction() 0 24 1
B detailAction() 0 58 7
1
<?php
2
3
/**
4
 * YAWIK
5
 *
6
 * @filesource
7
 * @license MIT
8
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
9
 */
10
11
namespace Organizations\Controller;
12
13
14
use Auth\Exception\UnauthorizedAccessException;
15
use Core\Entity\Exception\NotFoundException;
16
use Jobs\Repository\Job as JobRepository;
17
use Organizations\Entity\Organization;
18
use Organizations\Repository\Organization as OrganizationRepository;
19
use Zend\Http\Response;
20
use Zend\I18n\Translator\TranslatorInterface;
21
use Zend\Mvc\Controller\AbstractActionController;
22
use Zend\View\Model\ViewModel;
23
use Organizations\ImageFileCache\Manager as ImageFileCacheManager;
24
25
/**
26
 * Class ProfileController
27
 *
28
 * @author Anthonius Munthi <[email protected]>
29
 * @package Organizations\Controller
30
 * @since 0.30.1
31
 */
32
class ProfileController extends AbstractActionController
33
{
34
    /**
35
     * @var OrganizationRepository
36
     */
37
    private $repo;
38
39
    /**
40
     * @var JobRepository
41
     */
42
    private $jobRepo;
43
44
    /**
45
     * @var TranslatorInterface
46
     */
47
    private $translator;
48
49
    /**
50
     * @var ImageFileCacheManager
51
     */
52
    private $imageFileCacheManager;
53
54
    /**
55
     * @var array
56
     */
57
    private $options = [
58
        'count' => 10,
59
    ];
60
61
    public function __construct(
62
        OrganizationRepository $repo,
63
        JobRepository $jobRepository,
64
        TranslatorInterface $translator,
65
        ImageFileCacheManager $imageFileCacheManager,
66
        $options
67
    )
68
    {
69
        $this->repo = $repo;
70
        $this->translator = $translator;
71
        $this->imageFileCacheManager = $imageFileCacheManager;
72
        $this->jobRepo = $jobRepository;
73
        $this->options = $options;
74
    }
75
76
    /**
77
     * List organization
78
     *
79
     * @return ViewModel
80
     */
81
    public function indexAction()
82
    {
83
84
        $result = $this->pagination([
0 ignored issues
show
Documentation Bug introduced by
The method pagination does not exist on object<Organizations\Con...ller\ProfileController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
85
            'params' => ['Organizations_Profile',[
86
                    'q',
87
                    'count' => $this->options['count'],
88
                    'page' => 1,
89
                ]
90
            ],
91
            'paginator' => [
92
                'Organizations/Organization',
93
                'as' => 'organizations',
94
                'params' => [
95
                    'type' => 'profile',
96
                ]
97
            ],
98
            'form' => [
99
                'Core/Search',
100
                'as' => 'form',
101
            ]
102
        ]);
103
        return new ViewModel($result);
104
    }
105
106
    /**
107
     * @return array|ViewModel
108
     */
109
    public function detailAction()
110
    {
111
        $translator      = $this->translator;
112
        $repo            = $this->repo;
113
        $id              = $this->params('id');
114
115
        if(is_null($id)){
116
            $this->getResponse()->setStatusCode(Response::STATUS_CODE_404);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\ResponseInterface as the method setStatusCode() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Response, Zend\Http\Response, Zend\Http\Response\Stream.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
117
            return [
118
                'message' => $translator->translate('Can not access profile page without id'),
119
                'exception' => new \InvalidArgumentException('Null Organization Profile Id')
120
            ];
121
        }
122
123
        $organization = $repo->find($id);
124
        if(!$organization instanceof Organization){
125
            throw new NotFoundException($id);
126
        }
127
128
        if(
129
            Organization::PROFILE_DISABLED == $organization->getProfileSetting()
130
            || is_null($organization->getProfileSetting())
131
        ){
132
            throw new UnauthorizedAccessException(/*@translate*/ 'This Organization Profile is disabled');
133
        }
134
135
        $result = $this->pagination([
0 ignored issues
show
Documentation Bug introduced by
The method pagination does not exist on object<Organizations\Con...ller\ProfileController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
136
            'params' => [
137
                'Organization_Jobs',[
138
                    'q',
139
                    'organization_id' => $organization->getId(),
140
                    'count' => $this->options['count'],
141
                    'page' => 1,
142
                ],
143
            ],
144
            'paginator' => [
145
                'as' => 'jobs',
146
                'Organizations/ListJob',
147
            ],
148
        ]);
149
150
        if(
151
            Organization::PROFILE_ACTIVE_JOBS == $organization->getProfileSetting()
152
        ){
153
            /* @var \Zend\Paginator\Paginator $paginator */
154
            $paginator = $result['jobs'];
155
            $count = $paginator->getTotalItemCount();
156
            if(0===$count){
157
                throw new UnauthorizedAccessException($this->translator->translate('This Organization Profile is disabled'));
158
            }
159
        }
160
        $result['organization'] = $organization;
161
        $result['organizationImageCache'] = $this->imageFileCacheManager;
162
163
        /* @var \Zend\Mvc\Controller\Plugin\Url $url */
164
        $result['paginationControlRoute'] = 'lang/organizations/profileDetail';
165
        return new ViewModel($result);
166
    }
167
}
168