Completed
Push — develop ( 53a378...d50788 )
by
unknown
22:42 queued 11:18
created

ConsoleController::expireJobsAction()   C

Complexity

Conditions 9
Paths 35

Size

Total Lines 90
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 90
rs 5.1827
cc 9
eloc 60
nc 35
nop 0

How to fix   Long Method   

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
/**
4
 * YAWIK
5
 *
6
 * @filesource
7
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
8
 * @license   MIT
9
 */
10
11
/** ConsoleController of Jobs */
12
namespace Jobs\Controller;
13
14
use Zend\Mvc\Controller\AbstractActionController;
15
use Jobs\Entity\Job;
16
use Zend\Console\Request as ConsoleRequest;
17
use Zend\ProgressBar\ProgressBar;
18
use Core\Console\ProgressBar as CoreProgressBar;
19
use Zend\ProgressBar\Adapter\Console as ConsoleAdapter;
20
use Auth\Entity\UserInterface;
21
22
class ConsoleController extends AbstractActionController
23
{
24
25
    public function expireJobsAction()
26
    {
27
        
28
        $services     = $this->getServiceLocator();
29
        $repositories = $services->get('repositories');
30
        /* @var \Jobs\Repository\Job $jobsRepo */
31
        $jobsRepo     = $repositories->get('Jobs/Job');
32
        $filter       = \Zend\Json\Json::decode($this->params('filter', '{}'));
33
        $query        = array();
34
        $limit        = 10;
35
        foreach ($filter as $key => $value) {
36
            switch ($key) {
37
                case "limit":
38
                    $limit = $value;
39
                    break;
40
                    
41
                case "days":
42
                    $date = new \DateTime();
43
                    $date->modify('-' . (int) $value. ' day');
44
                    $q = array('$lt' => $date);
45
                    if (isset($query['datePublishStart.date'])) {
46
                        $query['datePublishStart.date']= array_merge(
47
                            $query['datePublishStart.date'],
48
                            $q
49
                        );
50
                    } else {
51
                        $query['datePublishStart.date'] = $q;
52
                    }
53
                    break;
54
                    
55
                default:
56
                    $query[$key] = $value;
57
                    break;
58
            }
59
        }
60
        $query['status.name'] = 'active';
61
62
        $jobs = $jobsRepo->findBy($query,null,$limit);
63
        $count = count($jobs);
64
65
        if (0 === $count) {
66
            return 'No jobs found.';
67
        }
68
        
69
        foreach ($repositories->getEventManager()->getListeners('preUpdate') as $listener) {
70
            $repositories->getEventManager()->removeEventListener('preUpdate', $listener);
71
        }
72
        
73
        
74
        echo "$count jobs found, which have to expire ...\n";
75
        
76
        $progress     = new ProgressBar(
77
            new ConsoleAdapter(
78
                array(
79
                'elements' => array(
80
                    ConsoleAdapter::ELEMENT_TEXT,
81
                    ConsoleAdapter::ELEMENT_BAR,
82
                    ConsoleAdapter::ELEMENT_PERCENT,
83
                    ConsoleAdapter::ELEMENT_ETA
84
                ),
85
                'textWidth' => 20,
86
                'barLeftChar' => '-',
87
                'barRightChar' => ' ',
88
                'barIndicatorChar' => '>',
89
                )
90
            ),
91
            0,
92
            count($jobs)
93
        );
94
95
        $i = 0;
96
97
        /* @var \Jobs\Entity\Job $job */
98
        foreach ($jobs as $job) {
99
            $progress->update($i++, 'Job ' . $i . ' / ' . $count);
100
101
            $job->changeStatus('expired');
102
103
            if (0 == $i % 500) {
104
                $progress->update($i, 'Write to database...');
105
                $repositories->flush();
106
            }
107
        }
108
        $progress->update($i, 'Write to database...');
109
        $repositories->flush();
110
        $progress->update($i, 'Done');
111
        $progress->finish();
112
113
        return PHP_EOL;
114
    }
115
    
116
    public function setpermissionsAction()
117
    {
118
        $services     = $this->getServiceLocator();
119
        $repositories = $services->get('repositories');
120
        $repository   = $repositories->get('Jobs/Job');
121
        $userRep      = $repositories->get('Auth/User');
122
        $jobs         = $repository->findAll();
123
        $count        = count($jobs);
124
        $progress     = new CoreProgressBar($count);
125
        $i            = 0;
126
        /* @var Job $job */
127
        foreach ($jobs as $job) {
128
            $progress->update($i++, 'Job ' . $i . ' / ' . $count);
129
            
130
            $permissions = $job->getPermissions();
131
            $user        = $job->getUser();
132
            if (!$user instanceof UserInterface) {
133
                continue;
134
            }
135
            try {
136
                $group       = $user->getGroup($job->getCompany());
0 ignored issues
show
Bug introduced by
The method getGroup() does not exist on Auth\Entity\UserInterface. Did you maybe mean getGroups()?

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...
137
            } catch (\Exception $e) {
138
                continue;
139
            }
140
            if ($group) {
141
                $permissions->grant($group, 'view');
142
            }
143
            foreach ($job->getApplications() as $application) {
144
                $progress->update($i, 'set app perms...');
145
                $perms = $application->getPermissions();
146
                $perms->inherit($permissions);
147
                $jobUser = $userRep->findOneByEmail($job->getContactEmail());
148
                if ($jobUser) {
149
                    $perms->grant($jobUser, 'change');
150
                }
151
            }
152
            if (0 == $i % 500) {
153
                $progress->update($i, 'write to database...');
154
                $repositories->flush();
155
            }
156
        }
157
        $progress->update($i, 'write to database...');
158
        $repositories->flush();
159
        $progress->finish();
160
        return PHP_EOL;
161
    }
162
}
163