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 Game 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 Game, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Game extends EventProvider implements ServiceManagerAwareInterface |
||
21 | { |
||
22 | /** |
||
23 | * |
||
24 | * @var GameMapperInterface |
||
25 | */ |
||
26 | protected $gameMapper; |
||
27 | |||
28 | /** |
||
29 | * |
||
30 | * @var EntryMapperInterface |
||
31 | */ |
||
32 | protected $entryMapper; |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | * @var ServiceManager |
||
37 | */ |
||
38 | protected $serviceManager; |
||
39 | |||
40 | /** |
||
41 | * |
||
42 | * @var UserServiceOptionsInterface |
||
43 | */ |
||
44 | protected $options; |
||
45 | |||
46 | protected $playerformMapper; |
||
47 | |||
48 | protected $invitationMapper; |
||
49 | |||
50 | protected $userMapper; |
||
51 | |||
52 | protected $anonymousIdentifier = null; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * |
||
57 | * This service is ready for all types of games |
||
58 | * |
||
59 | * @param array $data |
||
60 | * @param string $formClass |
||
61 | * @return \PlaygroundGame\Entity\Game |
||
62 | */ |
||
63 | public function createOrUpdate(array $data, $game, $formClass) |
||
328 | |||
329 | /** |
||
330 | * getActiveGames |
||
331 | * |
||
332 | * @return Array of PlaygroundGame\Entity\Game |
||
333 | */ |
||
334 | public function getActiveGames($displayHome = true, $classType = '', $order = '') |
||
397 | |||
398 | /** |
||
399 | * getAvailableGames : Games OnLine and not already played by $user |
||
400 | * |
||
401 | * @return Array of PlaygroundGame\Entity\Game |
||
402 | */ |
||
403 | public function getAvailableGames($user, $maxResults = 2) |
||
424 | |||
425 | View Code Duplication | public function getEntriesQuery($game) |
|
460 | |||
461 | public function getEntriesHeader($game) |
||
500 | |||
501 | /** |
||
502 | * getGameEntries : I create an array of entries based on playerData + header |
||
503 | * |
||
504 | * @return Array of PlaygroundGame\Entity\Game |
||
505 | */ |
||
506 | public function getGameEntries($header, $entries, $game) |
||
529 | |||
530 | /** |
||
531 | * getActiveSliderGames |
||
532 | * |
||
533 | * @return Array of PlaygroundGame\Entity\Game |
||
534 | */ |
||
535 | public function getActiveSliderGames() |
||
567 | |||
568 | /** |
||
569 | * getPrizeCategoryGames |
||
570 | * |
||
571 | * @return Array of PlaygroundGame\Entity\Game |
||
572 | */ |
||
573 | View Code Duplication | public function getPrizeCategoryGames($categoryid) |
|
585 | |||
586 | public function checkGame($identifier, $checkIfStarted = true) |
||
587 | { |
||
588 | $gameMapper = $this->getGameMapper(); |
||
589 | |||
590 | if (! $identifier) { |
||
591 | return false; |
||
592 | } |
||
593 | |||
594 | $game = $gameMapper->findByIdentifier($identifier); |
||
595 | |||
596 | // the game has not been found |
||
597 | if (! $game) { |
||
598 | return false; |
||
599 | } |
||
600 | |||
601 | // for preview stuff as admin |
||
602 | if ($this->isAllowed('game', 'edit')) { |
||
603 | $r =$this->getServiceManager()->get('request'); |
||
604 | if ($r->getQuery()->get('preview')) { |
||
605 | $game->setActive(true); |
||
606 | $game->setStartDate(null); |
||
607 | $game->setEndDate(null); |
||
608 | $game->setPublicationDate(null); |
||
609 | $game->setBroadcastPlatform(true); |
||
610 | |||
611 | // I don't want the game to be updated through any update during the preview mode. |
||
612 | // I mark it as readonly for Doctrine |
||
613 | $this->getServiceManager() |
||
614 | ->get('doctrine.entitymanager.orm_default') |
||
615 | ->getUnitOfWork() |
||
616 | ->markReadOnly($game); |
||
617 | |||
618 | return $game; |
||
619 | } |
||
620 | } |
||
621 | |||
622 | // The game is inactive |
||
623 | if (! $game->getActive()) { |
||
624 | return false; |
||
625 | } |
||
626 | |||
627 | // the game has not begun yet |
||
628 | if (! $game->isOpen()) { |
||
629 | return false; |
||
630 | } |
||
631 | |||
632 | // the game is finished and closed |
||
633 | if (! $game->isStarted() && $checkIfStarted) { |
||
634 | return false; |
||
635 | } |
||
636 | |||
637 | return $game; |
||
638 | } |
||
639 | |||
640 | /** |
||
641 | * Return the last entry of the user on this game, if it exists. |
||
642 | * An entry can be associated to : |
||
643 | * - A user account (a real one, linked to PlaygroundUser |
||
644 | * - An anonymous Identifier (based on one value of playerData (generally email)) |
||
645 | * - A cookie set on the player device (the less secure) |
||
646 | * If the active param is set, it can check if the entry is active or not. |
||
647 | * If the bonus param is set, it can check if the entry is a bonus or not. |
||
648 | * |
||
649 | * @param unknown $game |
||
650 | * @param string $user |
||
651 | * @param boolean $active |
||
652 | * @param boolean $bonus |
||
653 | * @return boolean |
||
654 | */ |
||
655 | public function checkExistingEntry($game, $user = null, $active = null, $bonus = null) |
||
656 | { |
||
657 | $search = array('game' => $game); |
||
658 | |||
659 | if ($user) { |
||
660 | $search['user'] = $user; |
||
661 | } elseif ($this->getAnonymousIdentifier()) { |
||
662 | $search['anonymousIdentifier'] = $this->getAnonymousIdentifier(); |
||
663 | $search['user'] = null; |
||
664 | } else { |
||
665 | $search['anonymousId'] = $this->getAnonymousId(); |
||
666 | $search['user'] = null; |
||
667 | } |
||
668 | |||
669 | if (! is_null($active)) { |
||
670 | $search['active'] = $active; |
||
671 | } |
||
672 | if (! is_null($bonus)) { |
||
673 | $search['bonus'] = $bonus; |
||
674 | } |
||
675 | |||
676 | $entry = $this->getEntryMapper()->findOneBy($search, array('updated_at' => 'desc')); |
||
677 | |||
678 | return $entry; |
||
679 | } |
||
680 | |||
681 | /* |
||
682 | * This function updates the entry with the player data after checking |
||
683 | * that the data are compliant with the formUser Game attribute |
||
684 | * |
||
685 | * The $data has to be a json object |
||
686 | */ |
||
687 | public function updateEntryPlayerForm($data, $game, $user, $entry, $mandatory = true) |
||
688 | { |
||
689 | $form = $this->createFormFromJson($game->getPlayerForm()->getForm(), 'playerForm'); |
||
690 | $form->setData($data); |
||
691 | |||
692 | if (!$mandatory) { |
||
693 | $filter = $form->getInputFilter(); |
||
694 | foreach ($form->getElements() as $element) { |
||
695 | try { |
||
696 | $elementInput = $filter->get($element->getName()); |
||
697 | $elementInput->setRequired(false); |
||
698 | $form->get($element->getName())->setAttribute('required', false); |
||
699 | } catch (\Zend\Form\Exception\InvalidElementException $e) { |
||
700 | } |
||
701 | } |
||
702 | } |
||
703 | |||
704 | if ($form->isValid()) { |
||
705 | $dataJson = json_encode($form->getData()); |
||
706 | |||
707 | if ($game->getAnonymousAllowed() && |
||
708 | $game->getAnonymousIdentifier() && |
||
709 | isset($data[$game->getAnonymousIdentifier()]) |
||
710 | ) { |
||
711 | $session = new \Zend\Session\Container('anonymous_identifier'); |
||
712 | if (empty($session->offsetGet('anonymous_identifier'))) { |
||
713 | $anonymousIdentifier = $data[$game->getAnonymousIdentifier()]; |
||
714 | |||
715 | $entry->setAnonymousIdentifier($anonymousIdentifier); |
||
716 | |||
717 | // I must transmit this info during the whole game workflow |
||
718 | $session->offsetSet('anonymous_identifier', $anonymousIdentifier); |
||
719 | } |
||
720 | } |
||
721 | |||
722 | $entry->setPlayerData($dataJson); |
||
723 | $this->getEntryMapper()->update($entry); |
||
724 | } else { |
||
725 | return false; |
||
726 | } |
||
727 | |||
728 | return true; |
||
729 | } |
||
730 | |||
731 | |||
732 | public function checkIsFan($game) |
||
733 | { |
||
734 | // If on Facebook, check if you have to be a FB fan to play the game |
||
735 | $session = new Container('facebook'); |
||
736 | |||
737 | if ($session->offsetExists('signed_request')) { |
||
738 | // I'm on Facebook |
||
739 | $sr = $session->offsetGet('signed_request'); |
||
740 | if ($sr['page']['liked'] == 1) { |
||
741 | return true; |
||
742 | } |
||
743 | } else { |
||
744 | // I'm not on Facebook |
||
745 | return true; |
||
746 | } |
||
747 | |||
748 | return false; |
||
749 | } |
||
750 | |||
751 | public function getAnonymousIdentifier() |
||
752 | { |
||
753 | if (is_null($this->anonymousIdentifier)) { |
||
754 | // If on Facebook, check if you have to be a FB fan to play the game |
||
755 | $session = new Container('anonymous_identifier'); |
||
756 | |||
757 | if ($session->offsetExists('anonymous_identifier')) { |
||
758 | $this->anonymousIdentifier = $session->offsetGet('anonymous_identifier'); |
||
759 | } else { |
||
760 | $this->anonymousIdentifier = false; |
||
761 | } |
||
762 | } |
||
763 | |||
764 | return $this->anonymousIdentifier; |
||
765 | } |
||
766 | |||
767 | /** |
||
768 | * errors : |
||
769 | * -1 : user not connected |
||
770 | * -2 : limit entry games for this user reached |
||
771 | * |
||
772 | * @param \PlaygroundGame\Entity\Game $game |
||
773 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
774 | * @return number unknown |
||
775 | */ |
||
776 | public function play($game, $user) |
||
777 | { |
||
778 | |||
779 | // certaines participations peuvent rester ouvertes. |
||
780 | // On autorise alors le joueur à reprendre là ou il en était |
||
781 | // par exemple les postvote... |
||
782 | $entry = $this->checkExistingEntry($game, $user, true); |
||
783 | |||
784 | if (! $entry) { |
||
785 | if ($this->hasReachedPlayLimit($game, $user)) { |
||
786 | return false; |
||
787 | } |
||
788 | |||
789 | $entry = new Entry(); |
||
790 | $entry->setGame($game); |
||
791 | $entry->setUser($user); |
||
792 | $entry->setPoints(0); |
||
793 | $entry->setIp($this->getIp()); |
||
794 | $entry->setAnonymousId($this->getAnonymousId()); |
||
795 | if ($this->getAnonymousIdentifier()) { |
||
796 | $entry->setAnonymousIdentifier($this->getAnonymousIdentifier()); |
||
797 | } |
||
798 | |||
799 | $entry = $this->getEntryMapper()->insert($entry); |
||
800 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
801 | 'user' => $user, |
||
802 | 'game' => $game, |
||
803 | 'entry' => $entry |
||
804 | )); |
||
805 | } |
||
806 | |||
807 | return $entry; |
||
808 | } |
||
809 | |||
810 | /** |
||
811 | * @param \PlaygroundGame\Entity\Game $game |
||
812 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
813 | */ |
||
814 | public function hasReachedPlayLimit($game, $user) |
||
829 | |||
830 | /** |
||
831 | * @param \PlaygroundGame\Entity\Game $game |
||
832 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
833 | */ |
||
834 | public function findLastEntries($game, $user, $limitScale) |
||
835 | { |
||
836 | $limitDate = $this->getLimitDate($limitScale); |
||
837 | |||
838 | if ($user) { |
||
839 | return $this->getEntryMapper()->findLastEntriesByUser($game, $user, $limitDate); |
||
840 | } elseif ($this->getAnonymousIdentifier()) { |
||
841 | return $this->getEntryMapper()->findLastEntriesByAnonymousIdentifier( |
||
842 | $game, |
||
843 | $this->getAnonymousIdentifier(), |
||
844 | $limitDate |
||
845 | ); |
||
846 | } else { |
||
847 | return $this->getEntryMapper()->findLastEntriesByIp($game, $this->getIp(), $limitDate); |
||
848 | } |
||
849 | } |
||
850 | |||
851 | /** |
||
852 | * |
||
853 | * |
||
854 | */ |
||
855 | public function getLimitDate($limitScale) |
||
890 | |||
891 | public function findLastActiveEntry($game, $user) |
||
895 | |||
896 | public function findLastInactiveEntry($game, $user) |
||
897 | { |
||
898 | return $this->checkExistingEntry($game, $user, false, false); |
||
899 | } |
||
900 | |||
901 | public function findLastEntry($game, $user) |
||
902 | { |
||
903 | return $this->checkExistingEntry($game, $user, null, false); |
||
904 | } |
||
905 | |||
906 | public function inviteToTeam($data, $game, $user){ |
||
966 | |||
967 | public function sendShareMail( |
||
968 | $data, |
||
969 | $game, |
||
970 | $user, |
||
971 | $entry, |
||
972 | $template = 'share_game', |
||
973 | $topic = null, |
||
974 | $userTimer = array(), |
||
975 | $subject = '' |
||
976 | ) { |
||
977 | $mailService = $this->getServiceManager()->get('playgroundgame_message'); |
||
978 | $mailSent = false; |
||
979 | $from = $this->getOptions()->getEmailFromAddress(); |
||
980 | |||
981 | if(empty($subject)){ |
||
982 | $subject = $this->getServiceManager()->get('translator')->translate( |
||
983 | $this->getOptions()->getShareSubjectLine(), |
||
984 | 'playgroundgame' |
||
985 | ); |
||
986 | } else { |
||
987 | $subject = $this->getServiceManager()->get('translator')->translate( |
||
988 | $subject, |
||
989 | 'playgroundgame' |
||
990 | ); |
||
991 | } |
||
992 | |||
993 | $renderer = $this->getServiceManager()->get('Zend\View\Renderer\RendererInterface'); |
||
994 | $skinUrl = $renderer->url( |
||
995 | 'frontend', |
||
996 | array(), |
||
997 | array('force_canonical' => true) |
||
998 | ); |
||
999 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true) . '####' . time()), 0, 15)); |
||
1000 | |||
1001 | if (! $topic) { |
||
1002 | $topic = $game->getTitle(); |
||
1003 | } |
||
1004 | |||
1005 | if(isset($data['email']) && !is_array($data['email'])) $data['email'] = array($data['email']); |
||
1006 | |||
1007 | foreach ($data['email'] as $to) { |
||
1008 | $mailSent = true; |
||
1009 | if (!empty($to)) { |
||
1010 | $message = $mailService->createHtmlMessage( |
||
1011 | $from, |
||
1012 | $to, |
||
1013 | $subject, |
||
1014 | 'playground-game/email/' . $template, |
||
1015 | array( |
||
1016 | 'game' => $game, |
||
1017 | 'data' => $data, |
||
1018 | 'from' => $from, |
||
1019 | 'to' => $to, |
||
1020 | 'secretKey' => $secretKey, |
||
1021 | 'skinUrl' => $skinUrl, |
||
1022 | 'userTimer' => $userTimer |
||
1023 | ) |
||
1024 | ); |
||
1025 | try { |
||
1026 | $mailService->send($message); |
||
1027 | } catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) { |
||
1028 | //$mailSent = false; |
||
1029 | } |
||
1030 | |||
1031 | if ($entry) { |
||
1032 | $shares = json_decode($entry->getSocialShares(), true); |
||
1033 | (!isset($shares['mail']))? $shares['mail'] = 1:$shares['mail'] += 1; |
||
1034 | } |
||
1035 | } |
||
1036 | } |
||
1037 | |||
1038 | if ($mailSent) { |
||
1039 | if ($entry) { |
||
1040 | $sharesJson = json_encode($shares); |
||
1041 | $entry->setSocialShares($sharesJson); |
||
1042 | $entry = $this->getEntryMapper()->update($entry); |
||
1043 | } |
||
1044 | |||
1045 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
1046 | 'user' => $user, |
||
1047 | 'topic' => $topic, |
||
1048 | 'secretKey' => $secretKey, |
||
1049 | 'data' => $data, |
||
1050 | 'game' => $game, |
||
1051 | 'entry' => $entry |
||
1052 | )); |
||
1053 | |||
1054 | return true; |
||
1055 | } |
||
1056 | |||
1057 | return false; |
||
1058 | } |
||
1059 | |||
1060 | /** |
||
1061 | * @param \PlaygroundGame\Entity\Game $game |
||
1062 | * @param \PlaygroundUser\Entity\User $user |
||
1063 | * @param Entry $entry |
||
1064 | * @param \PlaygroundGame\Entity\Prize $prize |
||
1065 | */ |
||
1066 | public function sendResultMail($game, $user, $entry, $template = 'entry', $prize = null) |
||
1067 | { |
||
1068 | $mailService = $this->getServiceManager()->get('playgroundgame_message'); |
||
1069 | $from = $this->getOptions()->getEmailFromAddress(); |
||
1070 | if ($user) { |
||
1071 | $to = $user->getEmail(); |
||
1072 | } elseif ($entry->getAnonymousIdentifier()) { |
||
1073 | $to = $entry->getAnonymousIdentifier(); |
||
1074 | } else { |
||
1075 | return false; |
||
1076 | } |
||
1077 | $subject = $game->getTitle(); |
||
1078 | $renderer = $this->getServiceManager()->get('Zend\View\Renderer\RendererInterface'); |
||
1079 | $skinUrl = $renderer->url( |
||
1080 | 'frontend', |
||
1081 | array(), |
||
1082 | array('force_canonical' => true) |
||
1083 | ); |
||
1084 | $message = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/' . $template, array( |
||
1085 | 'game' => $game, |
||
1086 | 'entry' => $entry, |
||
1087 | 'skinUrl' => $skinUrl, |
||
1088 | 'prize' => $prize |
||
1089 | )); |
||
1090 | $mailService->send($message); |
||
1091 | } |
||
1092 | |||
1093 | public function sendGameMail($game, $user, $post, $template = 'postvote') |
||
1094 | { |
||
1095 | $mailService = $this->getServiceManager()->get('playgroundgame_message'); |
||
1096 | $from = $this->getOptions()->getEmailFromAddress(); |
||
1097 | $to = $user->getEmail(); |
||
1098 | $subject = $this->getServiceManager()->get('translator')->translate( |
||
1099 | $this->getOptions()->getParticipationSubjectLine(), 'playgroundgame' |
||
1100 | ); |
||
1101 | $renderer = $this->getServiceManager()->get('Zend\View\Renderer\RendererInterface'); |
||
1102 | $skinUrl = $renderer->url( |
||
1103 | 'frontend', |
||
1104 | array(), |
||
1105 | array('force_canonical' => true) |
||
1106 | ); |
||
1107 | |||
1108 | $message = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/' . $template, array( |
||
1109 | 'game' => $game, |
||
1110 | 'post' => $post, |
||
1111 | 'skinUrl' => $skinUrl |
||
1112 | )); |
||
1113 | $mailService->send($message); |
||
1114 | } |
||
1115 | |||
1116 | View Code Duplication | public function postFbWall($secretKey, $game, $user, $entry) |
|
1117 | { |
||
1118 | $topic = $game->getTitle(); |
||
1119 | |||
1120 | $shares = json_decode($entry->getSocialShares(), true); |
||
1121 | if (!isset($shares['fbwall'])) { |
||
1122 | $shares['fbwall'] = 1; |
||
1123 | } else { |
||
1124 | $shares['fbwall'] += 1; |
||
1125 | } |
||
1126 | $sharesJson = json_encode($shares); |
||
1127 | $entry->setSocialShares($sharesJson); |
||
1128 | $entry = $this->getEntryMapper()->update($entry); |
||
1129 | |||
1130 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
1131 | 'user' => $user, |
||
1132 | 'game' => $game, |
||
1133 | 'secretKey' => $secretKey, |
||
1134 | 'topic' => $topic, |
||
1135 | 'entry' => $entry |
||
1136 | )); |
||
1137 | |||
1138 | return true; |
||
1139 | } |
||
1140 | |||
1141 | public function postFbRequest($secretKey, $game, $user, $entry, $to) |
||
1142 | { |
||
1143 | $shares = json_decode($entry->getSocialShares(), true); |
||
1144 | $to = explode(',', $to); |
||
1145 | if (!isset($shares['fbrequest'])) { |
||
1146 | $shares['fbrequest'] = count($to); |
||
1147 | } else { |
||
1148 | $shares['fbrequest'] += count($to); |
||
1149 | } |
||
1150 | $sharesJson = json_encode($shares); |
||
1151 | $entry->setSocialShares($sharesJson); |
||
1152 | $entry = $this->getEntryMapper()->update($entry); |
||
1153 | |||
1154 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
1155 | 'user' => $user, |
||
1156 | 'game' => $game, |
||
1157 | 'secretKey' => $secretKey, |
||
1158 | 'entry' => $entry, |
||
1159 | 'invites' => count($to) |
||
1160 | )); |
||
1161 | |||
1162 | return true; |
||
1163 | } |
||
1164 | |||
1165 | View Code Duplication | public function postTwitter($secretKey, $game, $user, $entry) |
|
1166 | { |
||
1167 | $topic = $game->getTitle(); |
||
1168 | |||
1169 | $shares = json_decode($entry->getSocialShares(), true); |
||
1170 | if (!isset($shares['fbrequest'])) { |
||
1171 | $shares['tweet'] = 1; |
||
1172 | } else { |
||
1173 | $shares['tweet'] += 1; |
||
1174 | } |
||
1175 | $sharesJson = json_encode($shares); |
||
1176 | $entry->setSocialShares($sharesJson); |
||
1177 | $entry = $this->getEntryMapper()->update($entry); |
||
1178 | |||
1179 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
1180 | 'user' => $user, |
||
1181 | 'game' => $game, |
||
1182 | 'secretKey' => $secretKey, |
||
1183 | 'topic' => $topic, |
||
1184 | 'entry' => $entry |
||
1185 | )); |
||
1186 | |||
1187 | return true; |
||
1188 | } |
||
1189 | |||
1190 | View Code Duplication | public function postGoogle($secretKey, $game, $user, $entry) |
|
1191 | { |
||
1192 | $topic = $game->getTitle(); |
||
1193 | |||
1194 | $shares = json_decode($entry->getSocialShares(), true); |
||
1195 | if (!isset($shares['fbrequest'])) { |
||
1196 | $shares['google'] = 1; |
||
1197 | } else { |
||
1198 | $shares['google'] += 1; |
||
1199 | } |
||
1200 | $sharesJson = json_encode($shares); |
||
1201 | $entry->setSocialShares($sharesJson); |
||
1202 | $entry = $this->getEntryMapper()->update($entry); |
||
1203 | |||
1204 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
1205 | 'user' => $user, |
||
1206 | 'game' => $game, |
||
1207 | 'secretKey' => $secretKey, |
||
1208 | 'topic' => $topic, |
||
1209 | 'entry' => $entry |
||
1210 | )); |
||
1211 | |||
1212 | return true; |
||
1213 | } |
||
1214 | |||
1215 | /** |
||
1216 | * Is it possible to trigger a bonus entry ? |
||
1217 | * |
||
1218 | * @param unknown_type $game |
||
1219 | * @param unknown_type $user |
||
1220 | */ |
||
1221 | public function allowBonus($game, $user) |
||
1241 | |||
1242 | public function addAnotherEntry($game, $user, $winner = 0) |
||
1256 | |||
1257 | /** |
||
1258 | * This bonus entry doesn't give points nor badges |
||
1259 | * It's just there to increase the chances during the Draw |
||
1260 | * Old Name playBonus |
||
1261 | * |
||
1262 | * @param PlaygroundGame\Entity\Game $game |
||
1263 | * @param unknown $user |
||
1264 | * @return boolean unknown |
||
1265 | */ |
||
1266 | public function addAnotherChance($game, $user, $winner = 0) |
||
1276 | |||
1277 | /** |
||
1278 | * This bonus entry doesn't give points nor badges but can play again |
||
1279 | * |
||
1280 | * @param PlaygroundGame\Entity\Game $game |
||
1281 | * @param user $user |
||
1282 | * @return boolean unknown |
||
1283 | */ |
||
1284 | public function playAgain($game, $user, $winner = 0) |
||
1285 | { |
||
1286 | if ($this->allowBonus($game, $user)) { |
||
1287 | $entry = $this->addAnotherEntry($game, $user, $winner); |
||
1288 | $entry->setActive(1); |
||
1289 | $entry = $this->getEntryMapper()->update($entry); |
||
1290 | if ($entry->getActive() == 1) { |
||
1291 | return true; |
||
1292 | } |
||
1293 | } |
||
1294 | |||
1295 | return false; |
||
1296 | } |
||
1297 | |||
1298 | /** |
||
1299 | * @param string $path |
||
1300 | */ |
||
1301 | public function uploadFile($path, $file) |
||
1302 | { |
||
1303 | $err = $file["error"]; |
||
1304 | $message = ''; |
||
1305 | if ($err > 0) { |
||
1306 | switch ($err) { |
||
1307 | case '1': |
||
1308 | $message .= 'Max file size exceeded. (php.ini)'; |
||
1309 | break; |
||
1310 | case '2': |
||
1311 | $message .= 'Max file size exceeded.'; |
||
1312 | break; |
||
1313 | case '3': |
||
1314 | $message .= 'File upload was only partial.'; |
||
1315 | break; |
||
1316 | case '4': |
||
1317 | $message .= 'No file was attached.'; |
||
1318 | break; |
||
1319 | case '7': |
||
1320 | $message .= 'File permission denied.'; |
||
1321 | break; |
||
1322 | default: |
||
1323 | $message .= 'Unexpected error occurs.'; |
||
1324 | } |
||
1325 | |||
1326 | return $err; |
||
1327 | } else { |
||
1328 | $fileNewname = $this->fileNewname($path, $file['name'], true); |
||
1329 | |||
1330 | if (isset($file["base64"])) { |
||
1331 | list(, $img) = explode(',', $file["base64"]); |
||
1332 | $img = str_replace(' ', '+', $img); |
||
1333 | $im = base64_decode($img); |
||
1334 | if ($im !== false) { |
||
1335 | // getimagesizefromstring |
||
1336 | file_put_contents($path . $fileNewname, $im); |
||
1337 | } else { |
||
1338 | return 1; |
||
1339 | } |
||
1340 | |||
1341 | return $fileNewname; |
||
1342 | } else { |
||
1343 | $adapter = new \Zend\File\Transfer\Adapter\Http(); |
||
1344 | // 1Mo |
||
1345 | $size = new Size(array( |
||
1346 | 'max' => 1024000 |
||
1347 | )); |
||
1348 | $is_image = new IsImage('jpeg,png,gif,jpg'); |
||
1349 | $adapter->setValidators(array( |
||
1350 | $size, |
||
1351 | $is_image |
||
1352 | ), $fileNewname); |
||
1353 | |||
1354 | if (! $adapter->isValid()) { |
||
1355 | return false; |
||
1356 | } |
||
1357 | |||
1358 | @move_uploaded_file($file["tmp_name"], $path . $fileNewname); |
||
1359 | } |
||
1360 | |||
1361 | |||
1362 | if (class_exists("Imagick")) { |
||
1363 | $ext = pathinfo($fileNewname, PATHINFO_EXTENSION); |
||
1364 | $img = new \Imagick($path . $fileNewname); |
||
1365 | $img->cropThumbnailImage(100, 100); |
||
1366 | $img->setImageCompression(\Imagick::COMPRESSION_JPEG); |
||
1367 | $img->setImageCompressionQuality(75); |
||
1368 | // Strip out unneeded meta data |
||
1369 | $img->stripImage(); |
||
1370 | $img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $fileNewname)); |
||
1371 | ErrorHandler::stop(true); |
||
1372 | } |
||
1373 | } |
||
1374 | |||
1375 | return $fileNewname; |
||
1376 | } |
||
1377 | |||
1378 | /** |
||
1379 | * @param string $path |
||
1380 | */ |
||
1381 | public function fileNewname($path, $filename, $generate = false) |
||
1382 | { |
||
1383 | $sanitize = new Sanitize(); |
||
1384 | $name = $sanitize->filter($filename); |
||
1385 | $newpath = $path . $name; |
||
1386 | |||
1387 | if ($generate) { |
||
1388 | if (file_exists($newpath)) { |
||
1389 | $filename = pathinfo($name, PATHINFO_FILENAME); |
||
1390 | $ext = pathinfo($name, PATHINFO_EXTENSION); |
||
1391 | |||
1392 | $name = $filename . '_' . rand(0, 99) . '.' . $ext; |
||
1393 | } |
||
1394 | } |
||
1395 | |||
1396 | unset($sanitize); |
||
1397 | |||
1398 | return $name; |
||
1399 | } |
||
1400 | |||
1401 | /** |
||
1402 | * This function returns the list of games, order by $type |
||
1403 | */ |
||
1404 | public function getQueryGamesOrderBy($type = 'createdAt', $order = 'DESC') |
||
1452 | |||
1453 | public function draw($game) |
||
1475 | |||
1476 | /** |
||
1477 | * getGameMapper |
||
1478 | * |
||
1479 | * @return GameMapperInterface |
||
1480 | */ |
||
1481 | View Code Duplication | public function getGameMapper() |
|
1489 | |||
1490 | /** |
||
1491 | * setGameMapper |
||
1492 | * |
||
1493 | * @param GameMapperInterface $gameMapper |
||
1494 | * @return Game |
||
1495 | */ |
||
1496 | public function setGameMapper(GameMapperInterface $gameMapper) |
||
1502 | |||
1503 | /** |
||
1504 | * getEntryMapper |
||
1505 | * |
||
1506 | * @return EntryMapperInterface |
||
1507 | */ |
||
1508 | public function getEntryMapper() |
||
1516 | |||
1517 | /** |
||
1518 | * setEntryMapper |
||
1519 | * |
||
1520 | * @param EntryMapperInterface $entryMapper |
||
1521 | * @return Game |
||
1522 | */ |
||
1523 | public function setEntryMapper($entryMapper) |
||
1529 | |||
1530 | public function setOptions(ModuleOptions $options) |
||
1536 | |||
1537 | public function getOptions() |
||
1546 | |||
1547 | /** |
||
1548 | * Retrieve service manager instance |
||
1549 | * |
||
1550 | * @return ServiceManager |
||
1551 | */ |
||
1552 | public function getServiceManager() |
||
1556 | |||
1557 | /** |
||
1558 | * Set service manager instance |
||
1559 | * |
||
1560 | * @param ServiceManager $serviceManager |
||
1561 | * @return Game |
||
1562 | */ |
||
1563 | public function setServiceManager(ServiceManager $serviceManager) |
||
1569 | |||
1570 | /** |
||
1571 | * @param string $str |
||
1572 | */ |
||
1573 | public function getExtension($str) |
||
1582 | |||
1583 | /** |
||
1584 | * @param string $extension |
||
1585 | */ |
||
1586 | public function getSrc($extension, $temp_path) |
||
1606 | |||
1607 | /** |
||
1608 | * @param string $extension |
||
1609 | * @param string $rep |
||
1610 | * @param integer $mini_width |
||
1611 | * @param integer $mini_height |
||
1612 | */ |
||
1613 | public function resize($tmp_file, $extension, $rep, $src, $mini_width, $mini_height) |
||
1646 | |||
1647 | public function getGameEntity() |
||
1651 | |||
1652 | /** |
||
1653 | * @param string $resource |
||
1654 | * @param string $privilege |
||
1655 | */ |
||
1656 | public function isAllowed($resource, $privilege = null) |
||
1662 | |||
1663 | public function getIp() |
||
1664 | { |
||
1665 | $ipaddress = ''; |
||
1666 | if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
||
1667 | $ipaddress = $_SERVER['HTTP_CLIENT_IP']; |
||
1668 | } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
||
1669 | $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
||
1670 | } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) { |
||
1671 | $ipaddress = $_SERVER['HTTP_X_FORWARDED']; |
||
1672 | } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) { |
||
1673 | $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; |
||
1674 | } elseif (isset($_SERVER['HTTP_FORWARDED'])) { |
||
1675 | $ipaddress = $_SERVER['HTTP_FORWARDED']; |
||
1676 | } elseif (isset($_SERVER['REMOTE_ADDR'])) { |
||
1677 | $ipaddress = $_SERVER['REMOTE_ADDR']; |
||
1678 | } else { |
||
1679 | $ipaddress = 'UNKNOWN'; |
||
1680 | } |
||
1681 | |||
1682 | return $ipaddress; |
||
1683 | } |
||
1684 | |||
1685 | public function getAnonymousId() |
||
1686 | { |
||
1687 | $anonymousId = ''; |
||
1688 | if ($_COOKIE && array_key_exists('pg_anonymous', $_COOKIE)) { |
||
1689 | $anonymousId = $_COOKIE['pg_anonymous']; |
||
1690 | } |
||
1691 | |||
1692 | return $anonymousId; |
||
1693 | } |
||
1694 | |||
1695 | /** |
||
1696 | * |
||
1697 | * |
||
1698 | * This service is ready for all types of games |
||
1699 | * |
||
1700 | * @param array $data |
||
1701 | * @return \PlaygroundGame\Entity\Game |
||
1702 | */ |
||
1703 | View Code Duplication | public function createForm(array $data, $game, $form = null) |
|
1733 | |||
1734 | /** |
||
1735 | * getCSV creates lines of CSV and returns it. |
||
1736 | */ |
||
1737 | public function getCSV($array) |
||
1748 | |||
1749 | public function getAttributes($attributes) |
||
1773 | |||
1774 | /** |
||
1775 | * @param \Zend\InputFilter\InputFilter $inputFilter |
||
1776 | */ |
||
1777 | public function decorate($element, $attr, $inputFilter) |
||
1840 | /** |
||
1841 | * Create a ZF2 Form from json data |
||
1842 | * @return Form |
||
1843 | */ |
||
1844 | public function createFormFromJson($jsonForm, $id = 'jsonForm') |
||
1845 | { |
||
1846 | $formPV = json_decode($jsonForm); |
||
1847 | |||
1848 | $form = new Form(); |
||
1849 | $form->setAttribute('id', $id); |
||
1850 | $form->setAttribute('enctype', 'multipart/form-data'); |
||
1851 | |||
1852 | $inputFilter = new \Zend\InputFilter\InputFilter(); |
||
1853 | $factory = new InputFactory(); |
||
1854 | |||
1855 | foreach ($formPV as $element) { |
||
1856 | View Code Duplication | if (isset($element->line_text)) { |
|
1857 | $attr = $this->getAttributes($element->line_text[0]); |
||
1858 | $element = new Element\Text($attr['name']); |
||
1859 | $element = $this->decorate($element, $attr, $inputFilter); |
||
1860 | $form->add($element); |
||
1861 | } |
||
1862 | View Code Duplication | if (isset($element->line_password)) { |
|
1863 | $attr = $this->getAttributes($element->line_password[0]); |
||
1864 | $element = new Element\Password($attr['name']); |
||
1865 | $element = $this->decorate($element, $attr, $inputFilter); |
||
1866 | $form->add($element); |
||
1867 | } |
||
1868 | View Code Duplication | if (isset($element->line_hidden)) { |
|
1869 | $attr = $this->getAttributes($element->line_hidden[0]); |
||
1870 | $element = new Element\Hidden($attr['name']); |
||
1871 | $element = $this->decorate($element, $attr, $inputFilter); |
||
1872 | $form->add($element); |
||
1873 | } |
||
1874 | View Code Duplication | if (isset($element->line_email)) { |
|
1875 | $attr = $this->getAttributes($element->line_email[0]); |
||
1876 | $element = new Element\Email($attr['name']); |
||
1877 | $element = $this->decorate($element, $attr, $inputFilter); |
||
1878 | $form->add($element); |
||
1879 | } |
||
1880 | View Code Duplication | if (isset($element->line_radio)) { |
|
1881 | $attr = $this->getAttributes($element->line_radio[0]); |
||
1882 | $element = new Element\Radio($attr['name']); |
||
1883 | |||
1884 | $element->setLabel($attr['label']); |
||
1885 | $element->setAttributes( |
||
1886 | array( |
||
1887 | 'name' => $attr['name'], |
||
1888 | 'required' => $attr['required'], |
||
1889 | 'allowEmpty'=> !$attr['required'], |
||
1890 | 'class' => $attr['class'], |
||
1891 | 'id' => $attr['id'] |
||
1892 | ) |
||
1893 | ); |
||
1894 | $values = array(); |
||
1895 | foreach ($attr['innerData'] as $value) { |
||
1896 | $values[] = $value->label; |
||
1897 | } |
||
1898 | $element->setValueOptions($values); |
||
1899 | |||
1900 | $options = array(); |
||
1901 | $options['encoding'] = 'UTF-8'; |
||
1902 | $options['disable_inarray_validator'] = true; |
||
1903 | |||
1904 | $element->setOptions($options); |
||
1905 | |||
1906 | $form->add($element); |
||
1907 | |||
1908 | $inputFilter->add($factory->createInput(array( |
||
1909 | 'name' => $attr['name'], |
||
1910 | 'required' => $attr['required'], |
||
1911 | 'allowEmpty' => !$attr['required'], |
||
1912 | ))); |
||
1913 | } |
||
1914 | View Code Duplication | if (isset($element->line_checkbox)) { |
|
1915 | $attr = $this->getAttributes($element->line_checkbox[0]); |
||
1916 | $element = new Element\MultiCheckbox($attr['name']); |
||
1917 | |||
1918 | $element->setLabel($attr['label']); |
||
1919 | $element->setAttributes( |
||
1920 | array( |
||
1921 | 'name' => $attr['name'], |
||
1922 | 'required' => $attr['required'], |
||
1923 | 'allowEmpty'=> !$attr['required'], |
||
1924 | 'class' => $attr['class'], |
||
1925 | 'id' => $attr['id'] |
||
1926 | ) |
||
1927 | ); |
||
1928 | |||
1929 | $values = array(); |
||
1930 | foreach ($attr['innerData'] as $value) { |
||
1931 | $values[] = $value->label; |
||
1932 | } |
||
1933 | $element->setValueOptions($values); |
||
1934 | $form->add($element); |
||
1935 | |||
1936 | $options = array(); |
||
1937 | $options['encoding'] = 'UTF-8'; |
||
1938 | $options['disable_inarray_validator'] = true; |
||
1939 | |||
1940 | $element->setOptions($options); |
||
1941 | |||
1942 | $inputFilter->add($factory->createInput(array( |
||
1943 | 'name' => $attr['name'], |
||
1944 | 'required' => $attr['required'], |
||
1945 | 'allowEmpty'=> !$attr['required'], |
||
1946 | ))); |
||
1947 | } |
||
1948 | View Code Duplication | if (isset($element->line_dropdown)) { |
|
1949 | $attr = $this->getAttributes($element->line_dropdown[0]); |
||
1950 | $element = new Element\Select($attr['name']); |
||
1951 | |||
1952 | $element->setLabel($attr['label']); |
||
1953 | $element->setAttributes( |
||
1954 | array( |
||
1955 | 'name' => $attr['name'], |
||
1956 | 'required' => $attr['required'], |
||
1957 | 'allowEmpty'=> !$attr['required'], |
||
1958 | 'class' => $attr['class'], |
||
1959 | 'id' => $attr['id'] |
||
1960 | ) |
||
1961 | ); |
||
1962 | $values = array(); |
||
1963 | foreach ($attr['dropdownValues'] as $value) { |
||
1964 | $values[] = $value->dropdown_label; |
||
1965 | } |
||
1966 | $element->setValueOptions($values); |
||
1967 | $form->add($element); |
||
1968 | |||
1969 | $options = array(); |
||
1970 | $options['encoding'] = 'UTF-8'; |
||
1971 | $options['disable_inarray_validator'] = true; |
||
1972 | |||
1973 | $element->setOptions($options); |
||
1974 | |||
1975 | $inputFilter->add($factory->createInput(array( |
||
1976 | 'name' => $attr['name'], |
||
1977 | 'required' => $attr['required'], |
||
1978 | 'allowEmpty' => !$attr['required'], |
||
1979 | ))); |
||
1980 | } |
||
1981 | View Code Duplication | if (isset($element->line_paragraph)) { |
|
1982 | $attr = $this->getAttributes($element->line_paragraph[0]); |
||
1983 | $element = new Element\Textarea($attr['name']); |
||
1984 | $element = $this->decorate($element, $attr, $inputFilter); |
||
1985 | $form->add($element); |
||
1986 | } |
||
1987 | if (isset($element->line_upload)) { |
||
1988 | $attr = $this->getAttributes($element->line_upload[0]); |
||
1989 | $element = new Element\File($attr['name']); |
||
1990 | |||
1991 | $element->setLabel($attr['label']); |
||
1992 | $element->setAttributes( |
||
1993 | array( |
||
1994 | 'name' => $attr['name'], |
||
1995 | 'required' => $attr['required'], |
||
1996 | 'class' => $attr['class'], |
||
1997 | 'id' => $attr['id'] |
||
1998 | ) |
||
1999 | ); |
||
2000 | $form->add($element); |
||
2001 | |||
2002 | $inputFilter->add($factory->createInput(array( |
||
2003 | 'name' => $attr['name'], |
||
2004 | 'required' => $attr['required'], |
||
2005 | 'validators' => array( |
||
2006 | array( |
||
2007 | 'name' => '\Zend\Validator\File\Size', |
||
2008 | 'options' => array('min' => $attr['filesizeMin'], 'max' => $attr['filesizeMax']) |
||
2009 | ), |
||
2010 | array( |
||
2011 | 'name' => '\Zend\Validator\File\Extension', |
||
2012 | 'options' => array( |
||
2013 | 'png,PNG,jpg,JPG,jpeg,JPEG,gif,GIF', |
||
2014 | 'messages' => array( |
||
2015 | \Zend\Validator\File\Extension::FALSE_EXTENSION =>'Veuillez télécharger une image' |
||
2016 | ) |
||
2017 | ) |
||
2018 | ), |
||
2019 | ), |
||
2020 | ))); |
||
2021 | } |
||
2022 | } |
||
2023 | |||
2024 | $form->setInputFilter($inputFilter); |
||
2025 | |||
2026 | return $form; |
||
2027 | } |
||
2028 | |||
2029 | /** |
||
2030 | * Send mail for winner and/or loser |
||
2031 | * @param \PlaygroundGame\Entity\Game $game |
||
2032 | * @param \PlaygroundUser\Entity\User $user |
||
2033 | * @param \PlaygroundGame\Entity\Entry $lastEntry |
||
2034 | * @param \PlaygroundGame\Entity\Prize $prize |
||
2035 | */ |
||
2036 | public function sendMail($game, $user, $lastEntry, $prize = null) |
||
2052 | |||
2053 | public function getPlayerFormMapper() |
||
2054 | { |
||
2055 | if (null === $this->playerformMapper) { |
||
2056 | $this->playerformMapper = $this->getServiceManager()->get('playgroundgame_playerform_mapper'); |
||
2057 | } |
||
2058 | |||
2059 | return $this->playerformMapper; |
||
2060 | } |
||
2061 | |||
2062 | public function setPlayerFormMapper($playerformMapper) |
||
2063 | { |
||
2064 | $this->playerformMapper = $playerformMapper; |
||
2065 | |||
2066 | return $this; |
||
2067 | } |
||
2068 | |||
2069 | public function getInvitationMapper() |
||
2077 | |||
2078 | public function setInvitationMapper($invitationMapper) |
||
2084 | |||
2085 | /** |
||
2086 | * getUserMapper |
||
2087 | * |
||
2088 | * @return UserMapperInterface |
||
2089 | */ |
||
2090 | public function getUserMapper() |
||
2098 | } |
||
2099 |
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.