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 $anonymousIdentifier = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * |
||
| 52 | * |
||
| 53 | * This service is ready for all types of games |
||
| 54 | * |
||
| 55 | * @param array $data |
||
| 56 | * @param string $entity |
||
|
|
|||
| 57 | * @param string $formClass |
||
| 58 | * @return \PlaygroundGame\Entity\Game |
||
| 59 | */ |
||
| 60 | public function createOrUpdate(array $data, $game, $formClass) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * getActiveGames |
||
| 328 | * |
||
| 329 | * @return Array of PlaygroundGame\Entity\Game |
||
| 330 | */ |
||
| 331 | public function getActiveGames($displayHome = true, $classType = '', $order = '') |
||
| 332 | { |
||
| 333 | $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default'); |
||
| 334 | $today = new \DateTime("now"); |
||
| 335 | $today = $today->format('Y-m-d') . ' 23:59:59'; |
||
| 336 | $orderBy = 'g.publicationDate'; |
||
| 337 | if ($order != '') { |
||
| 338 | $orderBy = 'g.'.$order; |
||
| 339 | } |
||
| 340 | |||
| 341 | $qb = $em->createQueryBuilder(); |
||
| 342 | $and = $qb->expr()->andx(); |
||
| 343 | $and->add( |
||
| 344 | $qb->expr()->orX( |
||
| 345 | $qb->expr()->lte('g.publicationDate', ':date'), |
||
| 346 | $qb->expr()->isNull('g.publicationDate') |
||
| 347 | ) |
||
| 348 | ); |
||
| 349 | $and->add( |
||
| 350 | $qb->expr()->orX( |
||
| 351 | $qb->expr()->gte('g.closeDate', ':date'), |
||
| 352 | $qb->expr()->isNull('g.closeDate') |
||
| 353 | ) |
||
| 354 | ); |
||
| 355 | $qb->setParameter('date', $today); |
||
| 356 | |||
| 357 | $and->add($qb->expr()->eq('g.active', '1')); |
||
| 358 | $and->add($qb->expr()->eq('g.broadcastPlatform', '1')); |
||
| 359 | |||
| 360 | if ($classType != '') { |
||
| 361 | $and->add($qb->expr()->eq('g.classType', ':classType')); |
||
| 362 | $qb->setParameter('classType', $classType); |
||
| 363 | } |
||
| 364 | |||
| 365 | if ($displayHome) { |
||
| 366 | $and->add($qb->expr()->eq('g.displayHome', true)); |
||
| 367 | } |
||
| 368 | |||
| 369 | $qb->select('g') |
||
| 370 | ->from('PlaygroundGame\Entity\Game', 'g') |
||
| 371 | ->where($and) |
||
| 372 | ->orderBy($orderBy, 'DESC'); |
||
| 373 | |||
| 374 | $query = $qb->getQuery(); |
||
| 375 | $games = $query->getResult(); |
||
| 376 | |||
| 377 | // je les classe par date de publication (date comme clé dans le tableau afin de pouvoir merger les objets |
||
| 378 | // de type article avec le même procédé en les classant naturellement par date asc ou desc |
||
| 379 | $arrayGames = array(); |
||
| 380 | View Code Duplication | foreach ($games as $game) { |
|
| 381 | if ($game->getPublicationDate()) { |
||
| 382 | $key = $game->getPublicationDate()->format('Ymd'); |
||
| 383 | } elseif ($game->getStartDate()) { |
||
| 384 | $key = $game->getStartDate()->format('Ymd'); |
||
| 385 | } else { |
||
| 386 | $key = $game->getUpdatedAt()->format('Ymd'); |
||
| 387 | } |
||
| 388 | $key .= $game->getUpdatedAt()->format('Ymd') . '-' . $game->getId(); |
||
| 389 | $arrayGames[$key] = $game; |
||
| 390 | } |
||
| 391 | |||
| 392 | return $arrayGames; |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * getAvailableGames : Games OnLine and not already played by $user |
||
| 397 | * |
||
| 398 | * @return Array of PlaygroundGame\Entity\Game |
||
| 399 | */ |
||
| 400 | public function getAvailableGames($user, $maxResults = 2) |
||
| 421 | |||
| 422 | View Code Duplication | public function getEntriesQuery($game) |
|
| 457 | |||
| 458 | public function getEntriesHeader($game) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * getGameEntries : I create an array of entries based on playerData + header |
||
| 500 | * |
||
| 501 | * @return Array of PlaygroundGame\Entity\Game |
||
| 502 | */ |
||
| 503 | public function getGameEntries($header, $entries, $game) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * getActiveSliderGames |
||
| 529 | * |
||
| 530 | * @return Array of PlaygroundGame\Entity\Game |
||
| 531 | */ |
||
| 532 | public function getActiveSliderGames() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * getPrizeCategoryGames |
||
| 567 | * |
||
| 568 | * @return Array of PlaygroundGame\Entity\Game |
||
| 569 | */ |
||
| 570 | View Code Duplication | public function getPrizeCategoryGames($categoryid) |
|
| 582 | |||
| 583 | public function checkGame($identifier, $checkIfStarted = true) |
||
| 584 | { |
||
| 585 | $gameMapper = $this->getGameMapper(); |
||
| 586 | |||
| 587 | if (! $identifier) { |
||
| 588 | return false; |
||
| 589 | } |
||
| 590 | |||
| 591 | $game = $gameMapper->findByIdentifier($identifier); |
||
| 592 | |||
| 593 | // the game has not been found |
||
| 594 | if (! $game) { |
||
| 595 | return false; |
||
| 596 | } |
||
| 597 | |||
| 598 | if ($this->isAllowed('game', 'edit')) { |
||
| 599 | $game->setActive(true); |
||
| 600 | $game->setStartDate(null); |
||
| 601 | $game->setEndDate(null); |
||
| 602 | $game->setPublicationDate(null); |
||
| 603 | $game->setBroadcastPlatform(true); |
||
| 604 | |||
| 605 | // I don't want the game to be updated through any update during the preview mode. |
||
| 606 | // I mark it as readonly for Doctrine |
||
| 607 | $this->getServiceManager() |
||
| 608 | ->get('doctrine.entitymanager.orm_default') |
||
| 609 | ->getUnitOfWork() |
||
| 610 | ->markReadOnly($game); |
||
| 611 | return $game; |
||
| 612 | } |
||
| 613 | |||
| 614 | // The game is inactive |
||
| 615 | if (! $game->getActive()) { |
||
| 616 | return false; |
||
| 617 | } |
||
| 618 | |||
| 619 | // the game has not begun yet |
||
| 620 | if (! $game->isOpen()) { |
||
| 621 | return false; |
||
| 622 | } |
||
| 623 | |||
| 624 | // the game is finished and closed |
||
| 625 | if (! $game->isStarted() && $checkIfStarted) { |
||
| 626 | return false; |
||
| 627 | } |
||
| 628 | |||
| 629 | return $game; |
||
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Return the last entry of the user on this game, if it exists. |
||
| 634 | * An entry can be associated to : |
||
| 635 | * - A user account (a real one, linked to PlaygroundUser |
||
| 636 | * - An anonymous Identifier (based on one value of playerData (generally email)) |
||
| 637 | * - A cookie set on the player device (the less secure) |
||
| 638 | * If the active param is set, it can check if the entry is active or not. |
||
| 639 | * If the bonus param is set, it can check if the entry is a bonus or not. |
||
| 640 | * |
||
| 641 | * @param unknown $game |
||
| 642 | * @param string $user |
||
| 643 | * @param boolean $active |
||
| 644 | * @param boolean $bonus |
||
| 645 | * @return boolean |
||
| 646 | */ |
||
| 647 | public function checkExistingEntry($game, $user = null, $active = null, $bonus = null) |
||
| 648 | { |
||
| 649 | $entry = false; |
||
| 650 | $search = array('game' => $game); |
||
| 651 | |||
| 652 | if ($user) { |
||
| 653 | $search['user'] = $user; |
||
| 654 | } elseif ($this->getAnonymousIdentifier()) { |
||
| 655 | $search['anonymousIdentifier'] = $this->getAnonymousIdentifier(); |
||
| 656 | $search['user'] = null; |
||
| 657 | } else { |
||
| 658 | $search['anonymousId'] = $this->getAnonymousId(); |
||
| 659 | $search['user'] = null; |
||
| 660 | } |
||
| 661 | |||
| 662 | if (! is_null($active)) { |
||
| 663 | $search['active'] = $active; |
||
| 664 | } |
||
| 665 | if (! is_null($bonus)) { |
||
| 666 | $search['bonus'] = $bonus; |
||
| 667 | } |
||
| 668 | |||
| 669 | $entry = $this->getEntryMapper()->findOneBy($search, array('updated_at' => 'desc')); |
||
| 670 | |||
| 671 | return $entry; |
||
| 672 | } |
||
| 673 | |||
| 674 | /* |
||
| 675 | * This function updates the entry with the player data after checking |
||
| 676 | * that the data are compliant with the formUser Game attribute |
||
| 677 | * |
||
| 678 | * The $data has to be a json object |
||
| 679 | */ |
||
| 680 | public function updateEntryPlayerForm($data, $game, $user, $entry, $mandatory = true) |
||
| 723 | |||
| 724 | |||
| 725 | public function checkIsFan($game) |
||
| 743 | |||
| 744 | public function getAnonymousIdentifier() |
||
| 759 | |||
| 760 | /** |
||
| 761 | * errors : |
||
| 762 | * -1 : user not connected |
||
| 763 | * -2 : limit entry games for this user reached |
||
| 764 | * |
||
| 765 | * @param \PlaygroundGame\Entity\Game $game |
||
| 766 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
| 767 | * @return number unknown |
||
| 768 | */ |
||
| 769 | public function play($game, $user) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @param \PlaygroundGame\Entity\Game $game |
||
| 805 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
| 806 | */ |
||
| 807 | public function hasReachedPlayLimit($game, $user) |
||
| 822 | |||
| 823 | /** |
||
| 824 | * @param \PlaygroundGame\Entity\Game $game |
||
| 825 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
| 826 | */ |
||
| 827 | public function findLastEntries($game, $user, $limitScale) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * |
||
| 846 | * |
||
| 847 | */ |
||
| 848 | public function getLimitDate($limitScale) |
||
| 883 | |||
| 884 | public function findLastActiveEntry($game, $user) |
||
| 888 | |||
| 889 | public function findLastInactiveEntry($game, $user) |
||
| 893 | |||
| 894 | public function findLastEntry($game, $user) |
||
| 898 | |||
| 899 | public function sendShareMail( |
||
| 900 | $data, |
||
| 901 | $game, |
||
| 902 | $user, |
||
| 903 | $entry, |
||
| 904 | $template = 'share_game', |
||
| 905 | $topic = null, |
||
| 906 | $userTimer = array() |
||
| 907 | ) { |
||
| 908 | |||
| 909 | $mailService = $this->getServiceManager()->get('playgroundgame_message'); |
||
| 910 | $mailSent = false; |
||
| 911 | $from = $this->getOptions()->getEmailFromAddress(); |
||
| 912 | $subject = $this->getOptions()->getShareSubjectLine(); |
||
| 913 | $renderer = $this->getServiceManager()->get('Zend\View\Renderer\RendererInterface'); |
||
| 914 | if ($user) { |
||
| 915 | $email = $user->getEmail(); |
||
| 916 | } elseif ($entry->getAnonymousIdentifier()) { |
||
| 917 | $email = $entry->getAnonymousIdentifier(); |
||
| 918 | } else { |
||
| 919 | $email = $from; |
||
| 920 | } |
||
| 921 | $skinUrl = $renderer->url( |
||
| 922 | 'frontend', |
||
| 923 | array(), |
||
| 924 | array('force_canonical' => true) |
||
| 925 | ); |
||
| 926 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true) . '####' . time()), 0, 15)); |
||
| 927 | |||
| 928 | if (! $topic) { |
||
| 929 | $topic = $game->getTitle(); |
||
| 930 | } |
||
| 931 | |||
| 932 | $shares = json_decode($entry->getSocialShares(), true); |
||
| 933 | |||
| 934 | if ($data['email1']) { |
||
| 935 | $mailSent = true; |
||
| 936 | $message = $mailService->createHtmlMessage( |
||
| 937 | $from, |
||
| 938 | $data['email1'], |
||
| 939 | $subject, |
||
| 940 | 'playground-game/email/' . $template, |
||
| 941 | array( |
||
| 942 | 'game' => $game, |
||
| 943 | 'email' => $email, |
||
| 944 | 'secretKey' => $secretKey, |
||
| 945 | 'skinUrl' => $skinUrl, |
||
| 946 | 'userTimer' => $userTimer |
||
| 947 | ) |
||
| 948 | ); |
||
| 949 | $mailService->send($message); |
||
| 950 | |||
| 951 | if (!isset($shares['mail'])) { |
||
| 952 | $shares['mail'] = 1; |
||
| 953 | } else { |
||
| 954 | $shares['mail'] += 1; |
||
| 955 | } |
||
| 956 | } |
||
| 957 | if ($data['email2'] && $data['email2'] != $data['email1']) { |
||
| 958 | $mailSent = true; |
||
| 959 | $message = $mailService->createHtmlMessage( |
||
| 960 | $from, |
||
| 961 | $data['email2'], |
||
| 962 | $subject, |
||
| 963 | 'playground-game/email/' . $template, |
||
| 964 | array( |
||
| 965 | 'game' => $game, |
||
| 966 | 'email' => $email, |
||
| 967 | 'secretKey' => $secretKey, |
||
| 968 | 'skinUrl' => $skinUrl, |
||
| 969 | 'userTimer' => $userTimer |
||
| 970 | ) |
||
| 971 | ); |
||
| 972 | $mailService->send($message); |
||
| 973 | |||
| 974 | if (!isset($shares['mail'])) { |
||
| 975 | $shares['mail'] = 1; |
||
| 976 | } else { |
||
| 977 | $shares['mail'] += 1; |
||
| 978 | } |
||
| 979 | } |
||
| 980 | if ($data['email3'] && $data['email3'] != $data['email2'] && $data['email3'] != $data['email1']) { |
||
| 981 | $mailSent = true; |
||
| 982 | $message = $mailService->createHtmlMessage( |
||
| 983 | $from, |
||
| 984 | $data['email3'], |
||
| 985 | $subject, |
||
| 986 | 'playground-game/email/' . $template, |
||
| 987 | array( |
||
| 988 | 'game' => $game, |
||
| 989 | 'email' => $email, |
||
| 990 | 'secretKey' => $secretKey, |
||
| 991 | 'skinUrl' => $skinUrl, |
||
| 992 | 'userTimer' => $userTimer |
||
| 993 | ) |
||
| 994 | ); |
||
| 995 | $mailService->send($message); |
||
| 996 | |||
| 997 | if (!isset($shares['mail'])) { |
||
| 998 | $shares['mail'] = 1; |
||
| 999 | } else { |
||
| 1000 | $shares['mail'] += 1; |
||
| 1001 | } |
||
| 1002 | } |
||
| 1003 | if ($data['email4'] && |
||
| 1004 | $data['email4'] != $data['email3'] && |
||
| 1005 | $data['email4'] != $data['email2'] && |
||
| 1006 | $data['email4'] != $data['email1'] |
||
| 1007 | ) { |
||
| 1008 | $mailSent = true; |
||
| 1009 | $message = $mailService->createHtmlMessage( |
||
| 1010 | $from, |
||
| 1011 | $data['email4'], |
||
| 1012 | $subject, |
||
| 1013 | 'playground-game/email/' . $template, |
||
| 1014 | array( |
||
| 1015 | 'game' => $game, |
||
| 1016 | 'email' => $email, |
||
| 1017 | 'secretKey' => $secretKey, |
||
| 1018 | 'skinUrl' => $skinUrl, |
||
| 1019 | 'userTimer' => $userTimer |
||
| 1020 | ) |
||
| 1021 | ); |
||
| 1022 | $mailService->send($message); |
||
| 1023 | |||
| 1024 | if (!isset($shares['mail'])) { |
||
| 1025 | $shares['mail'] = 1; |
||
| 1026 | } else { |
||
| 1027 | $shares['mail'] += 1; |
||
| 1028 | } |
||
| 1029 | } |
||
| 1030 | if ($data['email5'] && |
||
| 1031 | $data['email5'] != $data['email4'] && |
||
| 1032 | $data['email5'] != $data['email3'] && |
||
| 1033 | $data['email5'] != $data['email2'] && |
||
| 1034 | $data['email5'] != $data['email1'] |
||
| 1035 | ) { |
||
| 1036 | $mailSent = true; |
||
| 1037 | $message = $mailService->createHtmlMessage( |
||
| 1038 | $from, |
||
| 1039 | $data['email5'], |
||
| 1040 | $subject, |
||
| 1041 | 'playground-game/email/' . $template, |
||
| 1042 | array( |
||
| 1043 | 'game' => $game, |
||
| 1044 | 'email' => $email, |
||
| 1045 | 'secretKey' => $secretKey, |
||
| 1046 | 'skinUrl' => $skinUrl, |
||
| 1047 | 'userTimer' => $userTimer |
||
| 1048 | ) |
||
| 1049 | ); |
||
| 1050 | $mailService->send($message); |
||
| 1051 | |||
| 1052 | if (!isset($shares['mail'])) { |
||
| 1053 | $shares['mail'] = 1; |
||
| 1054 | } else { |
||
| 1055 | $shares['mail'] += 1; |
||
| 1056 | } |
||
| 1057 | } |
||
| 1058 | if ($mailSent) { |
||
| 1059 | $sharesJson = json_encode($shares); |
||
| 1060 | $entry->setSocialShares($sharesJson); |
||
| 1061 | $entry = $this->getEntryMapper()->update($entry); |
||
| 1062 | |||
| 1063 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
| 1064 | 'user' => $user, |
||
| 1065 | 'topic' => $topic, |
||
| 1066 | 'secretKey' => $secretKey, |
||
| 1067 | 'game' => $game, |
||
| 1068 | 'entry' => $entry |
||
| 1069 | )); |
||
| 1070 | |||
| 1071 | return true; |
||
| 1072 | } |
||
| 1073 | |||
| 1074 | return false; |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1079 | * @param \PlaygroundUser\Entity\User $user |
||
| 1080 | * @param Entry $entry |
||
| 1081 | * @param \PlaygroundGame\Entity\Prize $prize |
||
| 1082 | */ |
||
| 1083 | public function sendResultMail($game, $user, $entry, $template = 'entry', $prize = null) |
||
| 1109 | |||
| 1110 | public function sendGameMail($game, $user, $post, $template = 'postvote') |
||
| 1130 | |||
| 1131 | View Code Duplication | public function postFbWall($secretKey, $game, $user, $entry) |
|
| 1155 | |||
| 1156 | public function postFbRequest($secretKey, $game, $user, $entry, $to) |
||
| 1179 | |||
| 1180 | View Code Duplication | public function postTwitter($secretKey, $game, $user, $entry) |
|
| 1204 | |||
| 1205 | View Code Duplication | public function postGoogle($secretKey, $game, $user, $entry) |
|
| 1229 | |||
| 1230 | /** |
||
| 1231 | * Is it possible to trigger a bonus entry ? |
||
| 1232 | * |
||
| 1233 | * @param unknown_type $game |
||
| 1234 | * @param unknown_type $user |
||
| 1235 | */ |
||
| 1236 | public function allowBonus($game, $user) |
||
| 1256 | |||
| 1257 | public function addAnotherEntry($game, $user, $winner = 0) |
||
| 1258 | { |
||
| 1259 | $entry = new Entry(); |
||
| 1260 | $entry->setGame($game); |
||
| 1261 | $entry->setUser($user); |
||
| 1262 | $entry->setPoints(0); |
||
| 1263 | $entry->setIp($this->getIp()); |
||
| 1264 | $entry->setActive(0); |
||
| 1265 | $entry->setBonus(1); |
||
| 1266 | $entry->setWinner($winner); |
||
| 1267 | $entry = $this->getEntryMapper()->insert($entry); |
||
| 1268 | |||
| 1269 | return $entry; |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | /** |
||
| 1273 | * This bonus entry doesn't give points nor badges |
||
| 1274 | * It's just there to increase the chances during the Draw |
||
| 1275 | * Old Name playBonus |
||
| 1276 | * |
||
| 1277 | * @param PlaygroundGame\Entity\Game $game |
||
| 1278 | * @param unknown $user |
||
| 1279 | * @return boolean unknown |
||
| 1280 | */ |
||
| 1281 | public function addAnotherChance($game, $user, $winner = 0) |
||
| 1291 | |||
| 1292 | /** |
||
| 1293 | * This bonus entry doesn't give points nor badges but can play again |
||
| 1294 | * |
||
| 1295 | * @param PlaygroundGame\Entity\Game $game |
||
| 1296 | * @param user $user |
||
| 1297 | * @return boolean unknown |
||
| 1298 | */ |
||
| 1299 | public function playAgain($game, $user, $winner = 0) |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * @param string $path |
||
| 1315 | */ |
||
| 1316 | public function uploadFile($path, $file) |
||
| 1317 | { |
||
| 1318 | $err = $file["error"]; |
||
| 1319 | $message = ''; |
||
| 1320 | if ($err > 0) { |
||
| 1321 | switch ($err) { |
||
| 1322 | case '1': |
||
| 1323 | $message .= 'Max file size exceeded. (php.ini)'; |
||
| 1324 | break; |
||
| 1325 | case '2': |
||
| 1326 | $message .= 'Max file size exceeded.'; |
||
| 1327 | break; |
||
| 1328 | case '3': |
||
| 1329 | $message .= 'File upload was only partial.'; |
||
| 1330 | break; |
||
| 1331 | case '4': |
||
| 1332 | $message .= 'No file was attached.'; |
||
| 1333 | break; |
||
| 1334 | case '7': |
||
| 1335 | $message .= 'File permission denied.'; |
||
| 1336 | break; |
||
| 1337 | default: |
||
| 1338 | $message .= 'Unexpected error occurs.'; |
||
| 1339 | } |
||
| 1340 | |||
| 1341 | return $err; |
||
| 1342 | } else { |
||
| 1343 | $fileNewname = $this->fileNewname($path, $file['name'], true); |
||
| 1344 | |||
| 1345 | if (isset($file["base64"])) { |
||
| 1346 | list(, $img) = explode(',', $file["base64"]); |
||
| 1347 | $img = str_replace(' ', '+', $img); |
||
| 1348 | $im = base64_decode($img); |
||
| 1349 | if ($im !== false) { |
||
| 1350 | // getimagesizefromstring |
||
| 1351 | file_put_contents($path . $fileNewname, $im); |
||
| 1352 | } else { |
||
| 1353 | return 1; |
||
| 1354 | } |
||
| 1355 | |||
| 1356 | return $fileNewname; |
||
| 1357 | } else { |
||
| 1358 | $adapter = new \Zend\File\Transfer\Adapter\Http(); |
||
| 1359 | // 1Mo |
||
| 1360 | $size = new Size(array( |
||
| 1361 | 'max' => 1024000 |
||
| 1362 | )); |
||
| 1363 | $is_image = new IsImage('jpeg,png,gif,jpg'); |
||
| 1364 | $adapter->setValidators(array( |
||
| 1365 | $size, |
||
| 1366 | $is_image |
||
| 1367 | ), $fileNewname); |
||
| 1368 | |||
| 1369 | if (! $adapter->isValid()) { |
||
| 1370 | return false; |
||
| 1371 | } |
||
| 1372 | |||
| 1373 | @move_uploaded_file($file["tmp_name"], $path . $fileNewname); |
||
| 1374 | } |
||
| 1375 | |||
| 1376 | |||
| 1377 | if (class_exists("Imagick")) { |
||
| 1378 | $ext = pathinfo($fileNewname, PATHINFO_EXTENSION); |
||
| 1379 | $img = new \Imagick($path . $fileNewname); |
||
| 1380 | $img->cropThumbnailImage(100, 100); |
||
| 1381 | $img->setImageCompression(\Imagick::COMPRESSION_JPEG); |
||
| 1382 | $img->setImageCompressionQuality(75); |
||
| 1383 | // Strip out unneeded meta data |
||
| 1384 | $img->stripImage(); |
||
| 1385 | $img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $fileNewname)); |
||
| 1386 | ErrorHandler::stop(true); |
||
| 1387 | } |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | return $fileNewname; |
||
| 1391 | } |
||
| 1392 | |||
| 1393 | /** |
||
| 1394 | * @param string $path |
||
| 1395 | */ |
||
| 1396 | public function fileNewname($path, $filename, $generate = false) |
||
| 1415 | |||
| 1416 | /** |
||
| 1417 | * This function returns the list of games, order by $type |
||
| 1418 | */ |
||
| 1419 | public function getQueryGamesOrderBy($type = 'createdAt', $order = 'DESC') |
||
| 1467 | |||
| 1468 | public function draw($game) |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * getGameMapper |
||
| 1493 | * |
||
| 1494 | * @return GameMapperInterface |
||
| 1495 | */ |
||
| 1496 | public function getGameMapper() |
||
| 1504 | |||
| 1505 | /** |
||
| 1506 | * setGameMapper |
||
| 1507 | * |
||
| 1508 | * @param GameMapperInterface $gameMapper |
||
| 1509 | * @return Game |
||
| 1510 | */ |
||
| 1511 | public function setGameMapper(GameMapperInterface $gameMapper) |
||
| 1517 | |||
| 1518 | /** |
||
| 1519 | * getEntryMapper |
||
| 1520 | * |
||
| 1521 | * @return EntryMapperInterface |
||
| 1522 | */ |
||
| 1523 | public function getEntryMapper() |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * setEntryMapper |
||
| 1534 | * |
||
| 1535 | * @param EntryMapperInterface $entryMapper |
||
| 1536 | * @return Game |
||
| 1537 | */ |
||
| 1538 | public function setEntryMapper($entryMapper) |
||
| 1544 | |||
| 1545 | public function setOptions(ModuleOptions $options) |
||
| 1551 | |||
| 1552 | public function getOptions() |
||
| 1561 | |||
| 1562 | /** |
||
| 1563 | * Retrieve service manager instance |
||
| 1564 | * |
||
| 1565 | * @return ServiceManager |
||
| 1566 | */ |
||
| 1567 | public function getServiceManager() |
||
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Set service manager instance |
||
| 1574 | * |
||
| 1575 | * @param ServiceManager $serviceManager |
||
| 1576 | * @return Game |
||
| 1577 | */ |
||
| 1578 | public function setServiceManager(ServiceManager $serviceManager) |
||
| 1584 | |||
| 1585 | /** |
||
| 1586 | * @param string $str |
||
| 1587 | */ |
||
| 1588 | public function getExtension($str) |
||
| 1597 | |||
| 1598 | /** |
||
| 1599 | * @param string $extension |
||
| 1600 | */ |
||
| 1601 | public function getSrc($extension, $temp_path) |
||
| 1621 | |||
| 1622 | /** |
||
| 1623 | * @param string $extension |
||
| 1624 | * @param string $rep |
||
| 1625 | * @param integer $mini_width |
||
| 1626 | * @param integer $mini_height |
||
| 1627 | */ |
||
| 1628 | public function resize($tmp_file, $extension, $rep, $src, $mini_width, $mini_height) |
||
| 1661 | |||
| 1662 | public function getGameEntity() |
||
| 1666 | |||
| 1667 | /** |
||
| 1668 | * @param string $resource |
||
| 1669 | * @param string $privilege |
||
| 1670 | */ |
||
| 1671 | public function isAllowed($resource, $privilege = null) |
||
| 1677 | |||
| 1678 | public function getIp() |
||
| 1679 | { |
||
| 1680 | $ipaddress = ''; |
||
| 1681 | if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
||
| 1682 | $ipaddress = $_SERVER['HTTP_CLIENT_IP']; |
||
| 1683 | } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
||
| 1684 | $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
||
| 1685 | } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) { |
||
| 1686 | $ipaddress = $_SERVER['HTTP_X_FORWARDED']; |
||
| 1687 | } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) { |
||
| 1688 | $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; |
||
| 1689 | } elseif (isset($_SERVER['HTTP_FORWARDED'])) { |
||
| 1690 | $ipaddress = $_SERVER['HTTP_FORWARDED']; |
||
| 1691 | } elseif (isset($_SERVER['REMOTE_ADDR'])) { |
||
| 1692 | $ipaddress = $_SERVER['REMOTE_ADDR']; |
||
| 1693 | } else { |
||
| 1694 | $ipaddress = 'UNKNOWN'; |
||
| 1695 | } |
||
| 1696 | |||
| 1697 | return $ipaddress; |
||
| 1698 | } |
||
| 1699 | |||
| 1700 | public function getAnonymousId() |
||
| 1709 | |||
| 1710 | /** |
||
| 1711 | * |
||
| 1712 | * |
||
| 1713 | * This service is ready for all types of games |
||
| 1714 | * |
||
| 1715 | * @param array $data |
||
| 1716 | * @return \PlaygroundGame\Entity\Game |
||
| 1717 | */ |
||
| 1718 | View Code Duplication | public function createForm(array $data, $game, $form = null) |
|
| 1719 | { |
||
| 1720 | $title = ''; |
||
| 1721 | $description = ''; |
||
| 1722 | |||
| 1723 | if ($data['form_jsonified']) { |
||
| 1724 | $jsonPV = json_decode($data['form_jsonified']); |
||
| 1725 | foreach ($jsonPV as $element) { |
||
| 1726 | if ($element->form_properties) { |
||
| 1727 | $attributes = $element->form_properties[0]; |
||
| 1728 | $title = $attributes->title; |
||
| 1729 | $description = $attributes->description; |
||
| 1730 | |||
| 1731 | break; |
||
| 1732 | } |
||
| 1733 | } |
||
| 1734 | } |
||
| 1735 | if (! $form) { |
||
| 1736 | $form = new \PlaygroundGame\Entity\PlayerForm(); |
||
| 1737 | } |
||
| 1738 | $form->setGame($game); |
||
| 1739 | $form->setTitle($title); |
||
| 1740 | $form->setDescription($description); |
||
| 1741 | $form->setForm($data['form_jsonified']); |
||
| 1742 | $form->setFormTemplate($data['form_template']); |
||
| 1743 | |||
| 1744 | $form = $this->getPlayerFormMapper()->insert($form); |
||
| 1745 | |||
| 1746 | return $form; |
||
| 1747 | } |
||
| 1748 | |||
| 1749 | /** |
||
| 1750 | * getCSV creates lines of CSV and returns it. |
||
| 1751 | */ |
||
| 1752 | public function getCSV($array) |
||
| 1763 | |||
| 1764 | public function getAttributes($attributes) |
||
| 1788 | |||
| 1789 | public function decorate($element, $attr, $inputFilter) |
||
| 1852 | /** |
||
| 1853 | * Create a ZF2 Form from json data |
||
| 1854 | * @return Form |
||
| 1855 | */ |
||
| 1856 | public function createFormFromJson($jsonForm, $id = 'jsonForm') |
||
| 1857 | { |
||
| 1858 | $formPV = json_decode($jsonForm); |
||
| 1859 | |||
| 1860 | $form = new Form(); |
||
| 1861 | $form->setAttribute('id', $id); |
||
| 1862 | $form->setAttribute('enctype', 'multipart/form-data'); |
||
| 1863 | |||
| 1864 | $inputFilter = new \Zend\InputFilter\InputFilter(); |
||
| 1865 | $factory = new InputFactory(); |
||
| 1866 | |||
| 1867 | foreach ($formPV as $element) { |
||
| 1868 | View Code Duplication | if (isset($element->line_text)) { |
|
| 1869 | $attr = $this->getAttributes($element->line_text[0]); |
||
| 1870 | $element = new Element\Text($attr['name']); |
||
| 1871 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1872 | $form->add($element); |
||
| 1873 | } |
||
| 1874 | View Code Duplication | if (isset($element->line_password)) { |
|
| 1875 | $attr = $this->getAttributes($element->line_password[0]); |
||
| 1876 | $element = new Element\Password($attr['name']); |
||
| 1877 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1878 | $form->add($element); |
||
| 1879 | } |
||
| 1880 | View Code Duplication | if (isset($element->line_hidden)) { |
|
| 1881 | $attr = $this->getAttributes($element->line_hidden[0]); |
||
| 1882 | $element = new Element\Hidden($attr['name']); |
||
| 1883 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1884 | $form->add($element); |
||
| 1885 | } |
||
| 1886 | View Code Duplication | if (isset($element->line_email)) { |
|
| 1887 | $attr = $this->getAttributes($element->line_email[0]); |
||
| 1888 | $element = new Element\Email($attr['name']); |
||
| 1889 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1890 | $form->add($element); |
||
| 1891 | } |
||
| 1892 | View Code Duplication | if (isset($element->line_radio)) { |
|
| 1893 | $attr = $this->getAttributes($element->line_radio[0]); |
||
| 1894 | $element = new Element\Radio($attr['name']); |
||
| 1895 | |||
| 1896 | $element->setLabel($attr['label']); |
||
| 1897 | $element->setAttributes( |
||
| 1898 | array( |
||
| 1899 | 'name' => $attr['name'], |
||
| 1900 | 'required' => $attr['required'], |
||
| 1901 | 'allowEmpty'=> !$attr['required'], |
||
| 1902 | 'class' => $attr['class'], |
||
| 1903 | 'id' => $attr['id'] |
||
| 1904 | ) |
||
| 1905 | ); |
||
| 1906 | $values = array(); |
||
| 1907 | foreach ($attr['innerData'] as $value) { |
||
| 1908 | $values[] = $value->label; |
||
| 1909 | } |
||
| 1910 | $element->setValueOptions($values); |
||
| 1911 | |||
| 1912 | $options = array(); |
||
| 1913 | $options['encoding'] = 'UTF-8'; |
||
| 1914 | $options['disable_inarray_validator'] = true; |
||
| 1915 | |||
| 1916 | $element->setOptions($options); |
||
| 1917 | |||
| 1918 | $form->add($element); |
||
| 1919 | |||
| 1920 | $inputFilter->add($factory->createInput(array( |
||
| 1921 | 'name' => $attr['name'], |
||
| 1922 | 'required' => $attr['required'], |
||
| 1923 | 'allowEmpty' => !$attr['required'], |
||
| 1924 | ))); |
||
| 1925 | } |
||
| 1926 | View Code Duplication | if (isset($element->line_checkbox)) { |
|
| 1927 | $attr = $this->getAttributes($element->line_checkbox[0]); |
||
| 1928 | $element = new Element\MultiCheckbox($attr['name']); |
||
| 1929 | |||
| 1930 | $element->setLabel($attr['label']); |
||
| 1931 | $element->setAttributes( |
||
| 1932 | array( |
||
| 1933 | 'name' => $attr['name'], |
||
| 1934 | 'required' => $attr['required'], |
||
| 1935 | 'allowEmpty'=> !$attr['required'], |
||
| 1936 | 'class' => $attr['class'], |
||
| 1937 | 'id' => $attr['id'] |
||
| 1938 | ) |
||
| 1939 | ); |
||
| 1940 | |||
| 1941 | $values = array(); |
||
| 1942 | foreach ($attr['innerData'] as $value) { |
||
| 1943 | $values[] = $value->label; |
||
| 1944 | } |
||
| 1945 | $element->setValueOptions($values); |
||
| 1946 | $form->add($element); |
||
| 1947 | |||
| 1948 | $options = array(); |
||
| 1949 | $options['encoding'] = 'UTF-8'; |
||
| 1950 | $options['disable_inarray_validator'] = true; |
||
| 1951 | |||
| 1952 | $element->setOptions($options); |
||
| 1953 | |||
| 1954 | $inputFilter->add($factory->createInput(array( |
||
| 1955 | 'name' => $attr['name'], |
||
| 1956 | 'required' => $attr['required'], |
||
| 1957 | 'allowEmpty'=> !$attr['required'], |
||
| 1958 | ))); |
||
| 1959 | } |
||
| 1960 | View Code Duplication | if (isset($element->line_dropdown)) { |
|
| 1961 | $attr = $this->getAttributes($element->line_dropdown[0]); |
||
| 1962 | $element = new Element\Select($attr['name']); |
||
| 1963 | |||
| 1964 | $element->setLabel($attr['label']); |
||
| 1965 | $element->setAttributes( |
||
| 1966 | array( |
||
| 1967 | 'name' => $attr['name'], |
||
| 1968 | 'required' => $attr['required'], |
||
| 1969 | 'allowEmpty'=> !$attr['required'], |
||
| 1970 | 'class' => $attr['class'], |
||
| 1971 | 'id' => $attr['id'] |
||
| 1972 | ) |
||
| 1973 | ); |
||
| 1974 | $values = array(); |
||
| 1975 | foreach ($attr['dropdownValues'] as $value) { |
||
| 1976 | $values[] = $value->dropdown_label; |
||
| 1977 | } |
||
| 1978 | $element->setValueOptions($values); |
||
| 1979 | $form->add($element); |
||
| 1980 | |||
| 1981 | $options = array(); |
||
| 1982 | $options['encoding'] = 'UTF-8'; |
||
| 1983 | $options['disable_inarray_validator'] = true; |
||
| 1984 | |||
| 1985 | $element->setOptions($options); |
||
| 1986 | |||
| 1987 | $inputFilter->add($factory->createInput(array( |
||
| 1988 | 'name' => $attr['name'], |
||
| 1989 | 'required' => $attr['required'], |
||
| 1990 | 'allowEmpty' => !$attr['required'], |
||
| 1991 | ))); |
||
| 1992 | } |
||
| 1993 | View Code Duplication | if (isset($element->line_paragraph)) { |
|
| 1994 | $attr = $this->getAttributes($element->line_paragraph[0]); |
||
| 1995 | $element = new Element\Textarea($attr['name']); |
||
| 1996 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1997 | $form->add($element); |
||
| 1998 | } |
||
| 1999 | if (isset($element->line_upload)) { |
||
| 2000 | $attr = $this->getAttributes($element->line_upload[0]); |
||
| 2001 | $element = new Element\File($attr['name']); |
||
| 2002 | |||
| 2003 | $element->setLabel($attr['label']); |
||
| 2004 | $element->setAttributes( |
||
| 2005 | array( |
||
| 2006 | 'name' => $attr['name'], |
||
| 2007 | 'required' => $attr['required'], |
||
| 2008 | 'class' => $attr['class'], |
||
| 2009 | 'id' => $attr['id'] |
||
| 2010 | ) |
||
| 2011 | ); |
||
| 2012 | $form->add($element); |
||
| 2013 | |||
| 2014 | $inputFilter->add($factory->createInput(array( |
||
| 2015 | 'name' => $attr['name'], |
||
| 2016 | 'required' => $attr['required'], |
||
| 2017 | 'validators' => array( |
||
| 2018 | array( |
||
| 2019 | 'name' => '\Zend\Validator\File\Size', |
||
| 2020 | 'options' => array('min' => $attr['filesizeMin'], 'max' => $attr['filesizeMax']) |
||
| 2021 | ), |
||
| 2022 | array( |
||
| 2023 | 'name' => '\Zend\Validator\File\Extension', |
||
| 2024 | 'options' => array( |
||
| 2025 | 'png,PNG,jpg,JPG,jpeg,JPEG,gif,GIF', |
||
| 2026 | 'messages' => array( |
||
| 2027 | \Zend\Validator\File\Extension::FALSE_EXTENSION =>'Veuillez télécharger une image' |
||
| 2028 | ) |
||
| 2029 | ) |
||
| 2030 | ), |
||
| 2031 | ), |
||
| 2032 | ))); |
||
| 2033 | } |
||
| 2034 | } |
||
| 2035 | |||
| 2036 | $form->setInputFilter($inputFilter); |
||
| 2037 | |||
| 2038 | return $form; |
||
| 2039 | } |
||
| 2040 | |||
| 2041 | /** |
||
| 2042 | * Send mail for winner and/or loser |
||
| 2043 | * @param \PlaygroundGame\Entity\Game $game |
||
| 2044 | * @param \PlaygroundUser\Entity\User $user |
||
| 2045 | * @param \PlaygroundGame\Entity\Entry $lastEntry |
||
| 2046 | * @param \PlaygroundGame\Entity\Prize $prize |
||
| 2047 | */ |
||
| 2048 | public function sendMail($game, $user, $lastEntry, $prize = null) |
||
| 2064 | |||
| 2065 | public function getPlayerFormMapper() |
||
| 2073 | |||
| 2074 | public function setPlayerFormMapper($playerformMapper) |
||
| 2080 | } |
||
| 2081 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.