| Total Complexity | 58 |
| Total Lines | 377 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TicketService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TicketService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class TicketService |
||
| 22 | { |
||
| 23 | const CAN_BUY_TICKET = 'can buy ticket'; |
||
| 24 | const CAN_DOWNLOAD_TICKET = 'can download ticket'; |
||
| 25 | const TICKETS_SOLD_OUT = 'all tickets sold out'; |
||
| 26 | const CAN_WANNA_VISIT = 'can wanna visit'; |
||
| 27 | const WAIT_FOR_PAYMENT_RECEIVE = 'wit for payment receive'; |
||
| 28 | const PAID_FOR_ANOTHER = 'paid for another'; |
||
| 29 | const PAID_IS_RETURNED = 'paid is return'; |
||
| 30 | const EVENT_DONE = 'event done'; |
||
| 31 | const EVENT_DEFAULT_STATE = 'event default state'; |
||
| 32 | |||
| 33 | /** @var EntityManager */ |
||
| 34 | protected $em; |
||
| 35 | |||
| 36 | /** @var array */ |
||
| 37 | protected $paymentsConfig; |
||
| 38 | |||
| 39 | /** @var TranslatorInterface */ |
||
| 40 | protected $translator; |
||
| 41 | |||
| 42 | /** @var RouterInterface */ |
||
| 43 | protected $router; |
||
| 44 | |||
| 45 | /** @var TicketCostService */ |
||
| 46 | protected $ticketCostService; |
||
| 47 | |||
| 48 | /** @var TokenStorageInterface */ |
||
| 49 | protected $tokenStorage; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * TicketService constructor. |
||
| 53 | * |
||
| 54 | * @param EntityManager $em |
||
| 55 | * @param array $paymentsConfig |
||
| 56 | * @param TranslatorInterface $translator |
||
| 57 | * @param RouterInterface $router |
||
| 58 | * @param TicketCostService $ticketCostService |
||
| 59 | * @param TokenStorageInterface $tokenStorage |
||
| 60 | */ |
||
| 61 | public function __construct($em, $paymentsConfig, $translator, $router, $ticketCostService, TokenStorageInterface $tokenStorage) |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Check discount for ticket. |
||
| 73 | * |
||
| 74 | * @param Ticket $ticket |
||
| 75 | * |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function isMustBeDiscount($ticket) |
||
| 79 | { |
||
| 80 | $event = $ticket->getEvent(); |
||
| 81 | $user = $ticket->getUser(); |
||
| 82 | |||
| 83 | if (!$event->getUseDiscounts()) { |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | $paidPayments = $this->em->getRepository('StfalconEventBundle:Payment') |
||
| 88 | ->findPaidPaymentsForUser($user); |
||
| 89 | |||
| 90 | return \count($paidPayments) > 0; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set Ticket Amount with recalculate discount. |
||
| 95 | * |
||
| 96 | * @param Ticket $ticket |
||
| 97 | * @param float $amount |
||
| 98 | * @param bool $isMustBeDiscount |
||
| 99 | * @param TicketCost $currentTicketCost |
||
| 100 | */ |
||
| 101 | public function setTicketAmount($ticket, $amount, $isMustBeDiscount, $currentTicketCost) |
||
| 102 | { |
||
| 103 | $ticket->setAmountWithoutDiscount($amount); |
||
| 104 | $ticket->setAmount($amount); |
||
| 105 | $ticket->setTicketCost($currentTicketCost); |
||
| 106 | /** -1 flag means you need to discount in the configuration */ |
||
| 107 | $discount = $isMustBeDiscount ? -1 : 0; |
||
| 108 | $this->setTicketBestDiscount($ticket, $ticket->getPromoCode(), $discount); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set the best (from promo code or standard discount) discount for ticket. |
||
| 113 | * |
||
| 114 | * @param Ticket $ticket |
||
| 115 | * @param PromoCode $promoCode |
||
| 116 | * @param float $discount |
||
| 117 | * |
||
| 118 | * @return Ticket |
||
| 119 | */ |
||
| 120 | public function setTicketBestDiscount($ticket, $promoCode, $discount = -1) |
||
| 121 | { |
||
| 122 | if (-1 === $discount) { |
||
| 123 | $discount = (float) $this->paymentsConfig['discount']; |
||
| 124 | } |
||
| 125 | if ($promoCode instanceof PromoCode && $promoCode->getDiscountAmount() / 100 > $discount) { |
||
| 126 | $this->setTicketPromoCode($ticket, $promoCode); |
||
| 127 | } else { |
||
| 128 | $ticket->setPromoCode(null); |
||
| 129 | $this->setTicketDiscount($ticket, $discount); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $ticket; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Set Ticket promo-code. |
||
| 137 | * |
||
| 138 | * @param Ticket $ticket |
||
| 139 | * @param PromoCode $promoCode |
||
| 140 | * |
||
| 141 | * @return Ticket |
||
| 142 | */ |
||
| 143 | public function setTicketPromoCode($ticket, $promoCode) |
||
| 144 | { |
||
| 145 | $ticket->setPromoCode($promoCode); |
||
| 146 | $this->setTicketDiscount($ticket, $promoCode->getDiscountAmount() / 100); |
||
| 147 | $promoCode->incTmpUsedCount(); |
||
| 148 | |||
| 149 | return $ticket; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set ticket discount. |
||
| 154 | * |
||
| 155 | * @param Ticket $ticket |
||
| 156 | * @param float $discount |
||
| 157 | * |
||
| 158 | * @return Ticket |
||
| 159 | */ |
||
| 160 | public function setTicketDiscount($ticket, $discount) |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Create ticket for User and Event. |
||
| 173 | * |
||
| 174 | * @param Event $event |
||
| 175 | * @param User $user |
||
| 176 | * |
||
| 177 | * @return Ticket |
||
| 178 | */ |
||
| 179 | public function createTicket($event, $user) |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param Event $event |
||
| 194 | * @param string $position |
||
| 195 | * @param TicketCost $ticketCost |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public function getTicketHtmlData($event, $position, $ticketCost) |
||
| 398 | } |
||
| 399 | } |
||
| 400 |