Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PaywallService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PaywallService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class PaywallService |
||
23 | { |
||
24 | /** @var EntityManager */ |
||
25 | protected $em; |
||
26 | |||
27 | protected $userService; |
||
28 | |||
29 | /** |
||
30 | * @param EntityManager $em |
||
31 | */ |
||
32 | public function __construct(EntityManager $em, UserService $userService) |
||
33 | { |
||
34 | $this->em = $em; |
||
35 | $this->userService = $userService; |
||
36 | } |
||
37 | |||
38 | public function filterRanges(Subscription $subscription, $periodId) |
||
39 | { |
||
40 | $ranges = $subscription->getRanges()->filter(function (Duration $duration) use ($periodId) { |
||
41 | return $duration->getId() == $periodId; |
||
42 | }); |
||
43 | |||
44 | return $ranges->first(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Gets all available subscriptions by criteria. |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | public function getSubscriptionsByCriteria(SubscriptionCriteria $criteria, $returnQuery = false) |
||
53 | { |
||
54 | return $this->em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
55 | ->getListByCriteria($criteria, $returnQuery); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Gets all user subscriptions by criteria. |
||
60 | * |
||
61 | * @return mixed |
||
62 | */ |
||
63 | public function getUserSubscriptionsByCriteria(SubscriptionCriteria $criteria, $returnQuery = false) |
||
64 | { |
||
65 | return $this->getRepository() |
||
66 | ->getListByCriteria($criteria, $returnQuery) |
||
67 | ; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Gets currently logged in user's subscriptions. |
||
72 | * |
||
73 | * @param SubscriptionCriteria $criteria |
||
74 | * @param bool $returnQuery |
||
75 | * |
||
76 | * @return mixed |
||
77 | */ |
||
78 | public function getMySubscriptionsByCriteria(SubscriptionCriteria $criteria, $returnQuery = false) |
||
79 | { |
||
80 | return $this->getRepository() |
||
81 | ->getListByCriteria($criteria, $returnQuery, true) |
||
82 | ; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Filter my subscriptions. |
||
87 | * |
||
88 | * @param array $items |
||
89 | * |
||
90 | * @return ArrayCollection |
||
91 | */ |
||
92 | public function filterMySubscriptions(array $items = array()) |
||
93 | { |
||
94 | $orderItems = new ArrayCollection($items); |
||
95 | foreach ($orderItems as $item) { |
||
96 | foreach ($orderItems as $value) { |
||
97 | $parent = $item->getParent(); |
||
98 | if ($item->getSubscription()->getId() == $value->getSubscription()->getId() && |
||
99 | $parent == $value |
||
100 | ) { |
||
101 | $value->setProlonged(true); |
||
102 | $value->setToPay($parent->getToPay()); |
||
103 | $value->setExpireAt($parent->getExpireAt()); |
||
104 | $value->setDuration($parent->getDuration()); |
||
105 | $value->setDiscountTotal($parent->getDiscountTotal()); |
||
106 | $value->setActive($parent->isActive()); |
||
107 | $value->setCreatedAt($parent->getCreatedAt()); |
||
108 | $orderItems->removeElement($item); |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | return $orderItems; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Count subscriptions by given criteria. |
||
118 | * |
||
119 | * @param array $criteria |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | public function countBy(array $criteria) |
||
127 | |||
128 | /** |
||
129 | * Gets user's subscriptions repository. |
||
130 | * |
||
131 | * @return EntityRepository |
||
132 | */ |
||
133 | public function getRepository() |
||
137 | |||
138 | /** |
||
139 | * Gets subscriptions repository. |
||
140 | * |
||
141 | * @return EntityRepository |
||
142 | */ |
||
143 | public function getSubscriptionRepository() |
||
144 | { |
||
145 | return $this->em->getRepository('Newscoop\PaywallBundle\Entity\Subscription'); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Gets user's subscriptions for issues by given Id. |
||
150 | * |
||
151 | * @param int $id Subscription Id to search for |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public function getIssues($id) |
||
156 | { |
||
157 | $issues = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Issue') |
||
158 | ->findBy(array( |
||
159 | 'subscription' => $id, |
||
160 | )); |
||
161 | |||
162 | $issuesArray = array(); |
||
163 | foreach ($issues as $issue) { |
||
164 | $issueName = $this->em->getRepository('Newscoop\Entity\Issue')->findOneByNumber($issue->getIssueNumber())->getName(); |
||
165 | |||
166 | $issuesArray[] = array( |
||
167 | 'id' => $issue->getId(), |
||
168 | 'name' => $issueName, |
||
169 | 'language' => $issue->getLanguage()->getName(), |
||
170 | 'date' => $issue->getStartDate(), |
||
171 | 'days' => $issue->getDays(), |
||
172 | 'paid' => $issue->getPaidDays(), |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | return $issuesArray; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Gets user's subscriptions for sections by given Id. |
||
181 | * |
||
182 | * @param int $id Subscription Id to search for |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | View Code Duplication | public function getSections($id) |
|
207 | |||
208 | /** |
||
209 | * Gets user's subscriptions for articles by given Id. |
||
210 | * |
||
211 | * @param int $id Subscription Id to search for |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | View Code Duplication | public function getArticles($id) |
|
236 | |||
237 | /** |
||
238 | * Gets currently added user's Sections by given language Id and subscription Id. |
||
239 | * |
||
240 | * @param int $language Language Id to search for |
||
241 | * @param int $subscriptionId Subscription Id to search for |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | public function getSectionsByLanguageAndId($language, $subscriptionId) |
||
246 | { |
||
247 | $sections = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Section') |
||
248 | ->findBy(array( |
||
249 | 'language' => $language, |
||
250 | 'subscription' => $subscriptionId, |
||
251 | )); |
||
252 | |||
253 | return $sections; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Checks if user had trial. |
||
258 | * |
||
259 | * @param Newscoop\Entity\User $user User |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function userHadTrial($user) |
||
264 | { |
||
265 | $qb = $this->em->getRepository('Newscoop\PaywallBundle\Entity\UserSubscription') |
||
266 | ->createQueryBuilder('u'); |
||
267 | |||
268 | $qb->select('count(u.id)') |
||
269 | ->where('u.user = :user') |
||
270 | ->andWhere($qb->expr()->isNotNull('u.trial')) |
||
271 | ->setParameter('user', $user); |
||
272 | |||
273 | $userTrials = (int) $qb->getQuery()->getSingleScalarResult(); |
||
274 | |||
275 | return $userTrials > 1 ? true : false; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Checks if trial is valid. |
||
280 | * |
||
281 | * @param Newscoop\Entity\User $user User |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function validateTrial($user) |
||
286 | { |
||
287 | $trial = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Trial') |
||
288 | ->findOneBy(array( |
||
289 | 'user' => $user, |
||
290 | 'is_active' => true, |
||
291 | )); |
||
292 | |||
293 | if ($trial) { |
||
294 | $datetime = new \DateTime('now'); |
||
295 | //if trial expired |
||
296 | |||
297 | if ($trial->getFinishTrial() >= $datetime) { |
||
298 | return true; |
||
299 | } |
||
300 | |||
301 | // deactivate trial |
||
302 | $trial->setIsActive(false); |
||
303 | $this->em->flush(); |
||
304 | |||
305 | return false; |
||
306 | } |
||
307 | |||
308 | return false; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Checks if trial is active. |
||
313 | * |
||
314 | * @param Newscoop\Entity\User $user User |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function isTrialActive($user) |
||
319 | { |
||
320 | $trial = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Trial') |
||
321 | ->findOneBy(array( |
||
322 | 'user' => $user, |
||
323 | )); |
||
324 | |||
325 | if ($trial) { |
||
326 | return $trial->getIsActive(); |
||
327 | } |
||
328 | |||
329 | return false; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Deactivates trial. |
||
334 | * |
||
335 | * @param Newscoop\Entity\User $user User |
||
336 | * |
||
337 | * @return bool |
||
338 | */ |
||
339 | public function deactivateTrial($user) |
||
340 | { |
||
341 | $trial = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Trial') |
||
342 | ->findOneBy(array( |
||
343 | 'user' => $user, |
||
344 | 'is_active' => true, |
||
345 | )); |
||
346 | |||
347 | if ($trial) { |
||
348 | $trial->setIsActive(false); |
||
349 | $this->em->flush(); |
||
350 | |||
351 | return true; |
||
352 | } |
||
353 | |||
354 | return false; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Gets all available sections by given language Id. |
||
359 | * |
||
360 | * @param int $language Language Id to search for |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public function getSectionsByLanguageId($languageId) |
||
373 | |||
374 | /** |
||
375 | * Gets currently added user's Issues by given language Id and subscription Id. |
||
376 | * |
||
377 | * @param int $language Language Id to search for |
||
378 | * @param int $subscription_id Subscription Id to search for |
||
379 | * |
||
380 | * @return array |
||
381 | */ |
||
382 | View Code Duplication | public function getIssuesByLanguageAndId($language, $subscription_id) |
|
383 | { |
||
384 | $issues = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Issue') |
||
385 | ->findBy(array( |
||
386 | 'language' => $language, |
||
387 | 'subscription' => $subscription_id, |
||
388 | )); |
||
389 | |||
390 | return $issues; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Gets all available Issues by given language Id. |
||
395 | * |
||
396 | * @param int $languageId Language Id to search for |
||
397 | * |
||
398 | * @return array |
||
399 | */ |
||
400 | public function getIssuesByLanguageId($languageId) |
||
401 | { |
||
402 | $issues = $this->em->getRepository('Newscoop\Entity\Issue') |
||
403 | ->findBy(array( |
||
404 | 'language' => $languageId, |
||
405 | )); |
||
406 | |||
407 | return $issues; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Gets currently added user's Articles by given language Id and subscription Id. |
||
412 | * |
||
413 | * @param int $language Language Id to search for |
||
414 | * @param int $subscription_id Subscription Id to search for |
||
415 | * |
||
416 | * @return array |
||
417 | */ |
||
418 | View Code Duplication | public function getArticlesByLanguageAndId($language, $subscription_id) |
|
419 | { |
||
420 | $articles = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Article') |
||
421 | ->findBy(array( |
||
422 | 'language' => $language, |
||
423 | 'subscription' => $subscription_id, |
||
424 | )); |
||
425 | |||
426 | return $articles; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Gets all available Articles by given language Id. |
||
431 | * |
||
432 | * @param int $languageId Language Id to search for |
||
433 | * |
||
434 | * @return array |
||
435 | */ |
||
436 | public function getArticlesByLanguageId($languageId) |
||
437 | { |
||
438 | $articles = $this->em->getRepository('Newscoop\Entity\Article') |
||
439 | ->findBy(array( |
||
440 | 'language' => $languageId, |
||
441 | )); |
||
442 | |||
443 | return $articles; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Gets subscription details by given subscription Id. |
||
448 | * |
||
449 | * @param int $subscriptionId Subscription Id to search for |
||
450 | * |
||
451 | * @return array |
||
452 | */ |
||
453 | public function getSubscriptionDetails($subscriptionId) |
||
454 | { |
||
455 | $subscription = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
456 | ->createQueryBuilder('s') |
||
457 | ->select('s.type', 's.duration', 's.price', 's.currency', 'i.publication') |
||
458 | ->innerJoin('s.specification', 'i', 'WITH', 'i.subscription = :id') |
||
459 | ->where('s.id = :id AND s.is_active = true') |
||
460 | ->setParameter('id', $subscriptionId) |
||
461 | ->getQuery() |
||
462 | ->getArrayResult(); |
||
463 | |||
464 | return $subscription; |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Gets one defined subscription by given subscription Id. |
||
469 | * |
||
470 | * @param int $subscriptionId Subscription Id to search for |
||
471 | * |
||
472 | * @return entity object |
||
473 | */ |
||
474 | public function getOneSubscriptionById($subscriptionId) |
||
475 | { |
||
476 | $subscription = $this->em->getRepository('Newscoop\PaywallBundle\Entity\Subscription') |
||
477 | ->findOneBy(array( |
||
478 | 'id' => $subscriptionId, |
||
479 | )); |
||
480 | |||
481 | return $subscription; |
||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Activates Subscription by Id and returns its instance. |
||
486 | * |
||
487 | * @param int $id User subscription id |
||
488 | * |
||
489 | * @return UserSubscription |
||
490 | */ |
||
491 | View Code Duplication | public function activateById($id) |
|
492 | { |
||
493 | $subscription = $this->em->getRepository('Newscoop\PaywallBundle\Entity\UserSubscription') |
||
494 | ->findOneBy(array( |
||
495 | 'id' => $id, |
||
496 | )); |
||
497 | |||
498 | if ($subscription) { |
||
499 | $this->activateUserSubscription($subscription); |
||
500 | $this->em->flush(); |
||
501 | } |
||
502 | |||
503 | return $subscription; |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Activates user subscription. |
||
508 | * |
||
509 | * @param UserSubscription $subscription |
||
510 | */ |
||
511 | public function activateUserSubscription(UserSubscription $subscription) |
||
512 | { |
||
513 | $subscription->setActive(true); |
||
514 | $subscription->setType('P'); |
||
515 | $subscription->setProlonged(false); |
||
516 | $now = new \DateTime('now'); |
||
517 | if (!$subscription->getExpireAt()) { |
||
518 | $subscription->setExpireAt($this->getExpirationDate($subscription)); |
||
519 | $subscription->setCreatedAt($now); |
||
520 | } |
||
521 | |||
522 | if ($subscription->getParent()) { |
||
523 | $subscription->getParent()->setActive(false); |
||
524 | $subscription->getParent()->setExpireAt($now); |
||
525 | } |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Gets user subscription expiration date. |
||
530 | * |
||
531 | * @param UserSubscription $userSubscription User subscription |
||
532 | * |
||
533 | * @return DateTime |
||
534 | */ |
||
535 | public function getExpirationDate(UserSubscription $userSubscription) |
||
536 | { |
||
537 | $now = new \DateTime('now'); |
||
538 | $createdAt = $userSubscription->getCreatedAt(); |
||
539 | if ($userSubscription->getParent()) { |
||
540 | $createdAt = $userSubscription->getStartsAt(); |
||
541 | } |
||
542 | |||
543 | // diffrence in days between subscription create date |
||
544 | // and actual activation date |
||
545 | $startDate = $createdAt ?: $now; |
||
546 | $duration = $userSubscription->getDuration(); |
||
547 | $value = $duration['value']; |
||
548 | $attribute = $duration['attribute']; |
||
549 | $timeSpan = null; |
||
550 | switch ($attribute) { |
||
551 | View Code Duplication | case Duration::MONTHS: |
|
552 | $diffrence = (int) $now->diff($createdAt)->format('%m'); |
||
553 | $months = $value + $diffrence; |
||
554 | $timeSpan = new \DateInterval('P'.$months.'M'); |
||
555 | break; |
||
556 | View Code Duplication | case Duration::DAYS: |
|
557 | $daysDiffrence = (int) $now->diff($createdAt)->format('%a'); |
||
558 | $days = $value + $daysDiffrence; |
||
559 | $timeSpan = new \DateInterval('P'.$days.'D'); |
||
560 | break; |
||
561 | } |
||
562 | |||
563 | return $startDate->add($timeSpan); |
||
564 | } |
||
565 | |||
566 | /** |
||
567 | * Gets Subscription configuration(details) by given Subscription Id. |
||
568 | * |
||
569 | * @param int $subscriptionId Subscription id |
||
570 | * |
||
571 | * @return entity object |
||
572 | */ |
||
573 | public function getOneSubscriptionSpecification($subscriptionId) |
||
574 | { |
||
575 | $subscriptionSpec = $this->em->getRepository('Newscoop\PaywallBundle\Entity\SubscriptionSpecification') |
||
576 | ->findOneBy(array( |
||
577 | 'subscription' => $subscriptionId, |
||
578 | )); |
||
579 | |||
580 | return $subscriptionSpec; |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Checks if Subscription by given User Id and Subscription Id exists. |
||
585 | * |
||
586 | * @param int $userId User id |
||
587 | * @param int $subscriptionId Subscription id |
||
588 | * @param string $active Active on inactive subscription |
||
589 | * |
||
590 | * @return array |
||
591 | */ |
||
592 | View Code Duplication | public function getOneByUserAndSubscription($userId, $subscriptionId, $active = 'Y') |
|
593 | { |
||
594 | $subscription = $this->getRepository() |
||
595 | ->findOneBy(array( |
||
596 | 'user' => $userId, |
||
597 | 'subscription' => $subscriptionId, |
||
598 | 'active' => $active, |
||
599 | )); |
||
600 | |||
601 | if ($subscription) { |
||
602 | return $subscription; |
||
603 | } |
||
604 | |||
605 | return; |
||
606 | } |
||
607 | |||
608 | public function getOrderItemBy($id, $period = null) |
||
609 | { |
||
610 | return $this->getRepository()->getOrderItemBy($id, $this->getCurrentUser(), $period); |
||
611 | } |
||
612 | |||
613 | public function getCurrentUser() |
||
614 | { |
||
615 | return $this->userService->getCurrentUser(); |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * Get one user subscription by user. |
||
620 | * |
||
621 | * @param Newscoop\Entity\User|int $user User or user id |
||
622 | * |
||
623 | * @return UserSubscription|null |
||
624 | */ |
||
625 | View Code Duplication | public function getOneByUser($user) |
|
626 | { |
||
627 | $subscription = $this->em->getRepository('Newscoop\PaywallBundle\Entity\UserSubscription') |
||
628 | ->findOneBy(array( |
||
629 | 'user' => $user, |
||
630 | 'active' => 'Y', |
||
631 | )); |
||
632 | |||
633 | if ($subscription) { |
||
634 | return $subscription; |
||
635 | } |
||
636 | |||
637 | return; |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * Gets all sections diffrent from already added user's sections by given language |
||
642 | * and publication. |
||
643 | * |
||
644 | * @param int $languageId Language Id |
||
645 | * @param int $publicationId Publication Id |
||
646 | * |
||
647 | * @return array |
||
648 | */ |
||
649 | public function getDiffrentSectionsByLanguage($languageId, $publicationId) |
||
650 | { |
||
651 | $sections = $this->em->getRepository('Newscoop\Entity\Section') |
||
652 | ->createQueryBuilder('s') |
||
653 | ->select('s.number', 's.name') |
||
654 | ->where('s.language != :id AND s.publication = :publicationId') |
||
655 | ->setParameters(array( |
||
656 | 'id' => $languageId, |
||
657 | 'publicationId' => $publicationId, |
||
658 | )) |
||
659 | ->getQuery() |
||
660 | ->getArrayResult(); |
||
661 | |||
662 | return $sections; |
||
663 | } |
||
664 | |||
665 | /** |
||
666 | * Gets all issues diffrent from already added user's issues by given language |
||
667 | * and publication. |
||
668 | * |
||
669 | * @param int $languageId Language Id |
||
670 | * @param int $publicationId Publication Id |
||
671 | * |
||
672 | * @return array |
||
673 | */ |
||
674 | View Code Duplication | public function getDiffrentIssuesByLanguage($languageId, $publicationId) |
|
675 | { |
||
676 | $issues = $this->em->getRepository('Newscoop\Entity\Issue') |
||
677 | ->createQueryBuilder('i') |
||
678 | ->select('i.number', 'i.name') |
||
679 | ->where('i.language != :id AND i.publication = :publicationId') |
||
680 | ->setParameters(array( |
||
681 | 'id' => $languageId, |
||
682 | 'publicationId' => $publicationId, |
||
683 | )) |
||
684 | ->getQuery() |
||
685 | ->getArrayResult(); |
||
686 | |||
687 | return $issues; |
||
688 | } |
||
689 | |||
690 | /** |
||
691 | * Gets all articles diffrent from already added user's articles by given language |
||
692 | * and publication. |
||
693 | * |
||
694 | * @param int $languageId Language Id |
||
695 | * @param int $publicationId Publication Id |
||
696 | * |
||
697 | * @return array |
||
698 | */ |
||
699 | View Code Duplication | public function getDiffrentArticlesByLanguage($languageId, $publicationId) |
|
700 | { |
||
701 | $articles = $this->em->getRepository('Newscoop\Entity\Article') |
||
702 | ->createQueryBuilder('i') |
||
703 | ->select('i.number', 'i.name') |
||
704 | ->where('i.language != :id AND i.publication = :publicationId') |
||
705 | ->setParameters(array( |
||
706 | 'id' => $languageId, |
||
707 | 'publicationId' => $publicationId, |
||
708 | )) |
||
709 | ->getQuery() |
||
710 | ->getArrayResult(); |
||
711 | |||
712 | return $articles; |
||
713 | } |
||
714 | |||
715 | /** |
||
716 | * Update Subscription according to SubscritionData class. |
||
717 | * |
||
718 | * @param UserSubscription $subscription |
||
719 | * @param SubscriptionData $data |
||
720 | * |
||
721 | * @return Subscription |
||
722 | */ |
||
723 | public function update(UserSubscription $subscription, SubscriptionData $data) |
||
729 | |||
730 | private function apply(UserSubscription $subscription, SubscriptionData $data) |
||
731 | { |
||
732 | if ($data->userId) { |
||
733 | $user = $this->em->getRepository('Newscoop\Entity\User')->getOneActiveUser($data->userId, false)->getOneOrNullResult(); |
||
734 | if ($user) { |
||
735 | $subscription->setUser($user); |
||
736 | } |
||
737 | } |
||
738 | |||
739 | if ($data->publicationId) { |
||
740 | $publication = $this->em->getRepository('Newscoop\Entity\Publication')->findOneBy(array('id' => $data->publicationId)); |
||
741 | if ($publication) { |
||
742 | $subscription->setPublication($publication); |
||
743 | } |
||
744 | } |
||
745 | |||
746 | if ($data->toPay) { |
||
747 | $subscription->setToPay($data->toPay); |
||
748 | } |
||
749 | |||
750 | if (!empty($data->duration)) { |
||
751 | $subscription->setDuration($data->duration); |
||
752 | } |
||
753 | |||
754 | if (!empty($data->discount)) { |
||
755 | $subscription->setDiscount($data->discount); |
||
756 | } |
||
757 | |||
758 | if ($data->subscriptionId) { |
||
759 | $subscription->setSubscription($data->subscriptionId); |
||
760 | } |
||
761 | |||
762 | if ($data->currency) { |
||
763 | $subscription->setCurrency($data->currency); |
||
764 | } |
||
765 | |||
766 | if ($data->active) { |
||
767 | $subscription->setActive($data->active); |
||
768 | } |
||
769 | |||
770 | if ($data->type) { |
||
771 | $subscription->setType($data->type); |
||
772 | } |
||
773 | |||
774 | return $subscription; |
||
775 | } |
||
776 | |||
777 | public function save(UserSubscription $subscription) |
||
778 | { |
||
779 | $this->em->getConnection()->beginTransaction(); |
||
780 | try { |
||
781 | $this->em->persist($subscription); |
||
782 | $this->em->flush(); |
||
783 | |||
784 | $this->em->getConnection()->commit(); |
||
785 | } catch (\Exception $e) { |
||
786 | // Rollback |
||
787 | $this->em->getConnection()->rollback(); |
||
788 | throw $e; |
||
789 | } |
||
790 | } |
||
791 | |||
792 | /** |
||
793 | * Deactivates Subscription by Id and returns its instance. |
||
794 | * |
||
795 | * @param int $id - user subscription id |
||
796 | * |
||
797 | * @return UserSubscription |
||
798 | */ |
||
799 | public function deactivateById($id) |
||
800 | { |
||
801 | $subscription = $this->em->getRepository('Newscoop\PaywallBundle\Entity\UserSubscription') |
||
802 | ->findOneBy(array( |
||
803 | 'id' => $id, |
||
804 | )); |
||
805 | |||
806 | if ($subscription) { |
||
807 | $subscription->setActive(false); |
||
808 | $subscription->setType('T'); |
||
809 | $this->em->flush(); |
||
810 | } |
||
811 | |||
812 | return $subscription; |
||
813 | } |
||
814 | |||
815 | /** |
||
816 | * Removes Subscription by Id and returns its instance. |
||
817 | * |
||
818 | * @param int $id User subscription id |
||
819 | * |
||
820 | * @return UserSubscription |
||
821 | */ |
||
822 | View Code Duplication | public function deleteById($id) |
|
823 | { |
||
824 | $subscription = $this->em->getRepository('Newscoop\PaywallBundle\Entity\UserSubscription') |
||
825 | ->findOneBy(array( |
||
826 | 'id' => $id, |
||
827 | )); |
||
828 | |||
829 | if ($subscription) { |
||
830 | $this->em->remove($subscription); |
||
831 | $this->em->flush(); |
||
832 | } |
||
833 | |||
834 | return $subscription; |
||
835 | } |
||
836 | |||
837 | public function getOneById($id) |
||
838 | { |
||
839 | $subscription = $this->em->getRepository('Newscoop\PaywallBundle\Entity\UserSubscription')->findOneBy(array( |
||
840 | 'id' => $id, |
||
841 | )); |
||
842 | |||
843 | return $subscription; |
||
844 | } |
||
845 | |||
846 | public function getUserSubscriptionBySubscriptionId($id) |
||
847 | { |
||
848 | $subscription = $this->em->getRepository('Newscoop\PaywallBundle\Entity\UserSubscription')->findOneBy(array( |
||
849 | 'subscription' => $id, |
||
850 | )); |
||
851 | |||
852 | return $subscription; |
||
853 | } |
||
854 | |||
855 | public function getOneByUserAndPublication($userId, $publicationId) |
||
864 | |||
865 | public function create() |
||
871 | |||
872 | public function getArticleRepository() |
||
876 | |||
877 | public function getSectionRepository() |
||
881 | |||
882 | public function getLanguageRepository() |
||
886 | |||
887 | public function getIssueRepository() |
||
891 | } |
||
892 |
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.