sendOverdueTaskNotificationsToManagers()   B
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 7
nop 0
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Console;
13
14
use Jitamin\Foundation\Security\Role;
15
use Jitamin\Model\TaskModel;
16
use Symfony\Component\Console\Helper\Table;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Task overdue notification command class.
23
 */
24
class TaskOverdueNotificationCommand extends BaseCommand
25
{
26
    /**
27
     * Configure the console command.
28
     *
29
     * @return void
30
     */
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('notification:overdue-tasks')
35
            ->setDescription('Send notifications for overdue tasks')
36
            ->addOption('show', null, InputOption::VALUE_NONE, 'Show sent overdue tasks')
37
            ->addOption('group', null, InputOption::VALUE_NONE, 'Group all overdue tasks for one user (from all projects) in one email')
38
            ->addOption('manager', null, InputOption::VALUE_NONE, 'Send all overdue tasks to project manager(s) in one email');
39
    }
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @param InputInterface  $output
45
     * @param OutputInterface $output
46
     *
47
     * @return void
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        if ($input->getOption('group')) {
52
            $tasks = $this->sendGroupOverdueTaskNotifications();
53
        } elseif ($input->getOption('manager')) {
54
            $tasks = $this->sendOverdueTaskNotificationsToManagers();
55
        } else {
56
            $tasks = $this->sendOverdueTaskNotifications();
57
        }
58
59
        if ($input->getOption('show')) {
60
            $this->showTable($output, $tasks);
61
        }
62
    }
63
64
    /**
65
     * Show table.
66
     *
67
     * @param OutputInterface $output
68
     * @param array           $tasks
69
     *
70
     * @return void
71
     */
72
    public function showTable(OutputInterface $output, array $tasks)
73
    {
74
        $rows = [];
75
76
        foreach ($tasks as $task) {
77
            $rows[] = [
78
                $task['id'],
79
                $task['title'],
80
                date('Y-m-d', $task['date_due']),
81
                $task['project_id'],
82
                $task['project_name'],
83
                $task['assignee_name'] ?: $task['assignee_username'],
84
            ];
85
        }
86
87
        $table = new Table($output);
88
        $table
89
            ->setHeaders(['Id', 'Title', 'Due date', 'Project Id', 'Project name', 'Assignee'])
90
            ->setRows($rows)
91
            ->render();
92
    }
93
94
    /**
95
     * Send all overdue tasks for one user in one email.
96
     */
97 View Code Duplication
    public function sendGroupOverdueTaskNotifications()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $tasks = $this->taskFinderModel->getOverdueTasks();
0 ignored issues
show
Documentation introduced by
The property taskFinderModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
101
        foreach ($this->groupByColumn($tasks, 'owner_id') as $user_tasks) {
102
            $users = $this->userNotificationModel->getUsersWithNotificationEnabled($user_tasks[0]['project_id']);
0 ignored issues
show
Documentation introduced by
The property userNotificationModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
104
            foreach ($users as $user) {
105
                $this->sendUserOverdueTaskNotifications($user, $user_tasks);
106
            }
107
        }
108
109
        return $tasks;
110
    }
111
112
    /**
113
     * Send all overdue tasks in one email to project manager(s).
114
     */
115
    public function sendOverdueTaskNotificationsToManagers()
116
    {
117
        $tasks = $this->taskFinderModel->getOverdueTasks();
0 ignored issues
show
Documentation introduced by
The property taskFinderModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
119
        foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) {
120
            $users = $this->userNotificationModel->getUsersWithNotificationEnabled($project_id);
0 ignored issues
show
Documentation introduced by
The property userNotificationModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121
            $managers = [];
122
123
            foreach ($users as $user) {
124
                $role = $this->projectUserRoleModel->getUserRole($project_id, $user['id']);
0 ignored issues
show
Documentation introduced by
The property projectUserRoleModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
125
                if ($role == Role::PROJECT_MANAGER) {
126
                    $managers[] = $user;
127
                }
128
            }
129
130
            foreach ($managers as $manager) {
131
                $this->sendUserOverdueTaskNotificationsToManagers($manager, $project_tasks);
132
            }
133
        }
134
135
        return $tasks;
136
    }
137
138
    /**
139
     * Send overdue tasks.
140
     */
141 View Code Duplication
    public function sendOverdueTaskNotifications()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $tasks = $this->taskFinderModel->getOverdueTasks();
0 ignored issues
show
Documentation introduced by
The property taskFinderModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
144
145
        foreach ($this->groupByColumn($tasks, 'project_id') as $project_id => $project_tasks) {
146
            $users = $this->userNotificationModel->getUsersWithNotificationEnabled($project_id);
0 ignored issues
show
Documentation introduced by
The property userNotificationModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
147
148
            foreach ($users as $user) {
149
                $this->sendUserOverdueTaskNotifications($user, $project_tasks);
150
            }
151
        }
152
153
        return $tasks;
154
    }
155
156
    /**
157
     * Send overdue tasks for a given user.
158
     *
159
     * @param array $user
160
     * @param array $tasks
161
     */
162
    public function sendUserOverdueTaskNotifications(array $user, array $tasks)
163
    {
164
        $user_tasks = [];
165
        $project_names = [];
166
167
        foreach ($tasks as $task) {
168
            if ($this->userNotificationFilterModel->shouldReceiveNotification($user, ['task' => $task])) {
0 ignored issues
show
Documentation introduced by
The property userNotificationFilterModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
169
                $user_tasks[] = $task;
170
                $project_names[$task['project_id']] = $task['project_name'];
171
            }
172
        }
173
174
        if (!empty($user_tasks)) {
175
            $this->userNotificationModel->sendUserNotification(
0 ignored issues
show
Documentation introduced by
The property userNotificationModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
176
                $user,
177
                TaskModel::EVENT_OVERDUE,
178
                ['tasks' => $user_tasks, 'project_name' => implode(', ', $project_names)]
179
            );
180
        }
181
    }
182
183
    /**
184
     * Send overdue tasks for a project manager(s).
185
     *
186
     * @param array $manager
187
     * @param array $tasks
188
     */
189
    public function sendUserOverdueTaskNotificationsToManagers(array $manager, array $tasks)
190
    {
191
        $this->userNotificationModel->sendUserNotification(
0 ignored issues
show
Documentation introduced by
The property userNotificationModel does not exist on object<Jitamin\Console\T...dueNotificationCommand>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
192
            $manager,
193
            TaskModel::EVENT_OVERDUE,
194
            ['tasks' => $tasks, 'project_name' => $tasks[0]['project_name']]
195
        );
196
    }
197
198
    /**
199
     * Group a collection of records by a column.
200
     *
201
     * @param array  $collection
202
     * @param string $column
203
     *
204
     * @return array
205
     */
206
    public function groupByColumn(array $collection, $column)
207
    {
208
        $result = [];
209
210
        foreach ($collection as $item) {
211
            $result[$item[$column]][] = $item;
212
        }
213
214
        return $result;
215
    }
216
}
217