Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
namespace App\Shell;
use App\Mailer\ToolsStatusMailer;
use App\Model\Entity\User;
use App\Model\Table\ToolsStatusTable;
use App\Model\Table\UsersTable;
use Cake\Console\Shell;
/**
* Class ToolsShell
* @package App\Shell
* Loaned out tools reminder if not returned in given X days/weeks/months.
*
* @property ToolsStatusTable $ToolsStatus
* @property UsersTable $Users
*/
class ToolsShell extends Shell {
/**
* Init
*/
public function initialize()
{
parent::initialize();
$this->loadModel('ToolsStatus');
}
/**
* Print out a list of users and their related tools with prolonged checkouts
* @return int
*/
public function prolongedCheckouts() {
$this->out(print_r($this->_findProlongedCheckouts(), true));
return 0;
}
/**
* Send email to all users with prolonged tool checkouts
*/
public function prolongedCheckoutsSend() {
/** @var User[] $users */
$users = $this->_findProlongedCheckouts();
$toolsStatusMailer = new ToolsStatusMailer();
foreach($users as $user) {
$this->out('Mail sent');
$toolsStatusMailer->send('prolongedCheckouts', [$user]);
}
}
/**
* Collect all users with the related tools with prolonged checkouts
* @return array
*/
protected function _findProlongedCheckouts() {
return $this->ToolsStatus->find('usersWithProlongedCheckouts')->toArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment