Skip to content

Instantly share code, notes, and snippets.

@ivorobioff ivorobioff/code1.php
Last active May 9, 2016

Embed
What would you like to do?
<?php
namespace XXX\Core\Appraisal\Services;
use XXX\Core\Appraisal\Criteria\FilterResolver;
use XXX\Core\Appraisal\Entities\Order;
use XXX\Core\Appraisal\Enums\BadgeType;
use XXX\Core\Appraisal\Enums\ProcessStatus;
use XXX\Core\Appraisal\Objects\Badge;
use XXX\Core\Support\Criteria\Constraint;
use XXX\Core\Support\Criteria\Criteria;
use XXX\Core\Support\Criteria\Filter;
use XXX\Core\Support\Service\AbstractService;
use RuntimeException;
use DateTime;
/**
* @author Igor Vorobiov <igor.vorobioff@gmail.com>
*/
class CalendarService extends AbstractService
{
/**
* @param int $appraiserId
* @param DateTime $from
* @param DateTime $to
* @return Badge[]
*/
public function getAllBadgesWithDayScale($appraiserId, DateTime $from = null, DateTime $to = null)
{
$builder = $this->entityManager->createQueryBuilder();
$builder
->select('o')
->from(Order::class, 'o')
->where($builder->expr()->eq('o.appraiser', ':appraiser'))
->setParameter('appraiser', $appraiserId);
$criteria[] = new Criteria('calendar.from', new Constraint(Constraint::GREATER_OR_EQUAL), $from);
$criteria[] = new Criteria('calendar.to', new Constraint(Constraint::LESS_OR_EQUAL), $to);
(new Filter())->apply($builder, $criteria, new FilterResolver());
/**
* @var Order[] $orders
*/
$orders = $builder->getQuery()->getResult();
$badges = [];
foreach ($orders as $order){
foreach ($this->getTypes($order) as $type){
$position = $this->getPosition($order, $type);
$group = $type.'-'.implode('-', $position);
if (!isset($badges[$group])){
$badge = new Badge();
$badge->setPosition($position);
$badge->setType($type);
$badges[$group] = $badge;
} else {
$badge = $badges[$group];
}
$badge->increaseCounter();
$badge->addOrder($order);
}
}
return $badges;
}
/**
* @param Order $order
* @return BadgeType[]
*/
private function getTypes(Order $order)
{
$types = [];
if ($order->getProcessStatus()->is(ProcessStatus::FRESH)){
$types[] = new BadgeType(BadgeType::FRESH);
}
if ($order->getProcessStatus()->is(ProcessStatus::REQUEST_FOR_BID)){
$types[] = new BadgeType(BadgeType::REQUEST_FOR_BID);
}
if ($order->getProcessStatus()->is(ProcessStatus::INSPECTION_SCHEDULED)){
$types[] = new BadgeType(BadgeType::INSPECTION_SCHEDULED);
}
if ($order->getDueDate() !== null){
$types[] = new BadgeType(BadgeType::DUE);
}
return $types;
}
/**
* @param Order $order
* @param BadgeType $type
* @return array
*/
private function getPosition(Order $order, BadgeType $type)
{
if ($type->is(BadgeType::DUE)){
$occurredAt = $order->getDueDate();
} elseif ($type->is(BadgeType::INSPECTION_SCHEDULED)){
$occurredAt = $order->getInspectionScheduledAt();
} elseif ($type->is([BadgeType::FRESH, BadgeType::REQUEST_FOR_BID])){
$occurredAt = $order->getOrderedAt();
} else {
throw new RuntimeException('Unknown badge type');
}
return [
(int) $occurredAt->format('Y'),
(int) $occurredAt->format('m'),
(int) $occurredAt->format('d')
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.