Created
March 27, 2013 23:50
-
-
Save merk/5259238 to your computer and use it in GitHub Desktop.
Example Twig Extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Ibms\JobBundle\Twig; | |
use Doctrine\Common\Collections\Collection; | |
use Ibms\JobBundle\Entity\Job; | |
use Ibms\JobBundle\Entity\JobAppliance; | |
use Ibms\UserBundle\Entity\Technician; | |
use DateTime; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Twig_Extension; | |
class JobExtension extends Twig_Extension | |
{ | |
private $container; | |
public function __construct(ContainerInterface $container) | |
{ | |
$this->container = $container; | |
} | |
public function getFunctions() | |
{ | |
return array( | |
'ibms_job_current' => new \Twig_Function_Method($this, 'isJobCurrent'), | |
'ibms_job_count' => new \Twig_Function_Method($this, 'getTechCount'), | |
'ibms_job_can_service' => new \Twig_Function_Method($this, 'canService'), | |
'ibms_job_warranty_errors' => new \Twig_Function_Method($this, 'getWarrantyErrors'), | |
'ibms_job_attendance_units' => new \Twig_Function_Method($this, 'getAttendanceUnits'), | |
'ibms_job_default_rate' => new \Twig_Function_Method($this, 'getJobRate'), | |
); | |
} | |
public function getJobRate() | |
{ | |
return $this->container->getParameter('ibms_job.job.default_rate'); | |
} | |
public function getWarrantyErrors(JobAppliance $jobAppliance) | |
{ | |
if (!($validator = $this->getValidator())) { | |
return array(); | |
} | |
return $validator->validateClaim($jobAppliance); | |
} | |
public function isJobCurrent(Job $job, DateTime $day, Technician $technician = null) | |
{ | |
$current = false; | |
if ($job->getScheduled() and $job->getScheduled()->format('Y/m/d') == $day->format('Y/m/d')) { | |
$current = true; | |
} | |
$current &= $job->getTechnician() === $technician; | |
return $current; | |
} | |
public function getTechCount(DateTime $day, Technician $technician = null) | |
{ | |
return $this->getUserManager()->getBookingsForDay($day, $technician); | |
} | |
public function canService(Technician $technician, Job $job) | |
{ | |
return $this->getDecider()->canService($technician, $job); | |
} | |
public function getAttendanceUnits(Collection $attendances) | |
{ | |
return $this->getCalculator()->calculateRateUnits($attendances->toArray()); | |
} | |
/** | |
* @return \Ibms\JobBundle\Technician\Decider | |
*/ | |
public function getDecider() | |
{ | |
return $this->container->get('ibms_job.technician.decider'); | |
} | |
/** | |
* @return \Ibms\JobBundle\Warranty\Validator | |
*/ | |
public function getValidator() | |
{ | |
return $this->container->get('ibms_job.warranty_validator'); | |
} | |
/** | |
* @return \Ibms\JobBundle\Attendance\Calculator | |
*/ | |
public function getCalculator() | |
{ | |
return $this->container->get('ibms_job.attendance.calculator'); | |
} | |
/** | |
* @return \Ibms\UserBundle\Entity\UserManager | |
*/ | |
public function getUserManager() | |
{ | |
return $this->container->get('fos_user.user_manager'); | |
} | |
public function getName() | |
{ | |
return 'ibms_job'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment