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 |
||
| 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 UserServiceOptionsInterface |
||
| 37 | */ |
||
| 38 | protected $options; |
||
| 39 | |||
| 40 | protected $playerformMapper; |
||
| 41 | |||
| 42 | protected $invitationMapper; |
||
| 43 | |||
| 44 | protected $userMapper; |
||
| 45 | |||
| 46 | protected $anonymousIdentifier = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * |
||
| 50 | * @var ServiceManager |
||
| 51 | */ |
||
| 52 | protected $serviceLocator; |
||
| 53 | |||
| 54 | public function __construct(ServiceLocatorInterface $locator) |
||
| 58 | |||
| 59 | View Code Duplication | public function getGameUserPath($game, $user) |
|
| 73 | |||
| 74 | View Code Duplication | public function getGameUserMediaUrl($game, $user) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * |
||
| 84 | * |
||
| 85 | * This service is ready for all types of games |
||
| 86 | * |
||
| 87 | * @param array $data |
||
| 88 | * @param string $formClass |
||
| 89 | * @return \PlaygroundGame\Entity\Game |
||
| 90 | */ |
||
| 91 | public function createOrUpdate(array $data, $game, $formClass) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * getActiveGames |
||
| 359 | * |
||
| 360 | * @return Array of PlaygroundGame\Entity\Game |
||
| 361 | */ |
||
| 362 | public function getActiveGames($displayHome = true, $classType = '', $order = '') |
||
| 363 | { |
||
| 364 | $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
||
| 365 | $today = new \DateTime("now"); |
||
| 366 | $today = $today->format('Y-m-d H:i:s'); |
||
| 367 | $orderBy = 'g.publicationDate'; |
||
| 368 | if ($order != '') { |
||
| 369 | $orderBy = 'g.'.$order; |
||
| 370 | } |
||
| 371 | |||
| 372 | $qb = $em->createQueryBuilder(); |
||
| 373 | $and = $qb->expr()->andx(); |
||
| 374 | $and->add( |
||
| 375 | $qb->expr()->orX( |
||
| 376 | $qb->expr()->lte('g.publicationDate', ':date'), |
||
| 377 | $qb->expr()->isNull('g.publicationDate') |
||
| 378 | ) |
||
| 379 | ); |
||
| 380 | $and->add( |
||
| 381 | $qb->expr()->orX( |
||
| 382 | $qb->expr()->gte('g.closeDate', ':date'), |
||
| 383 | $qb->expr()->isNull('g.closeDate') |
||
| 384 | ) |
||
| 385 | ); |
||
| 386 | $qb->setParameter('date', $today); |
||
| 387 | |||
| 388 | $and->add($qb->expr()->eq('g.active', '1')); |
||
| 389 | $and->add($qb->expr()->eq('g.broadcastPlatform', '1')); |
||
| 390 | |||
| 391 | if ($classType != '') { |
||
| 392 | $and->add($qb->expr()->eq('g.classType', ':classType')); |
||
| 393 | $qb->setParameter('classType', $classType); |
||
| 394 | } |
||
| 395 | |||
| 396 | if ($displayHome) { |
||
| 397 | $and->add($qb->expr()->eq('g.displayHome', true)); |
||
| 398 | } |
||
| 399 | |||
| 400 | $qb->select('g') |
||
| 401 | ->from('PlaygroundGame\Entity\Game', 'g') |
||
| 402 | ->where($and) |
||
| 403 | ->orderBy($orderBy, 'DESC'); |
||
| 404 | |||
| 405 | $query = $qb->getQuery(); |
||
| 406 | $games = $query->getResult(); |
||
| 407 | |||
| 408 | // je les classe par date de publication (date comme clé dans le tableau afin de pouvoir merger les objets |
||
| 409 | // de type article avec le même procédé en les classant naturellement par date asc ou desc |
||
| 410 | $arrayGames = array(); |
||
| 411 | View Code Duplication | foreach ($games as $game) { |
|
| 412 | if ($game->getPublicationDate()) { |
||
| 413 | $key = $game->getPublicationDate()->format('Ymd'); |
||
| 414 | } elseif ($game->getStartDate()) { |
||
| 415 | $key = $game->getStartDate()->format('Ymd'); |
||
| 416 | } else { |
||
| 417 | $key = $game->getUpdatedAt()->format('Ymd'); |
||
| 418 | } |
||
| 419 | $key .= $game->getUpdatedAt()->format('Ymd') . '-' . $game->getId(); |
||
| 420 | $arrayGames[$key] = $game; |
||
| 421 | } |
||
| 422 | |||
| 423 | return $arrayGames; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * getAvailableGames : Games OnLine and not already played by $user |
||
| 428 | * |
||
| 429 | * @return Array of PlaygroundGame\Entity\Game |
||
| 430 | */ |
||
| 431 | public function getAvailableGames($user, $maxResults = 2) |
||
| 432 | { |
||
| 433 | $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
||
| 434 | $today = new \DateTime("now"); |
||
| 435 | $today = $today->format('Y-m-d H:i:s'); |
||
| 436 | |||
| 437 | // Game active with a start_date before today (or without start_date) |
||
| 438 | // and end_date after today (or without end-date) |
||
| 439 | $query = $em->createQuery('SELECT g FROM PlaygroundGame\Entity\Game g |
||
| 440 | WHERE NOT EXISTS (SELECT l FROM PlaygroundGame\Entity\Entry l WHERE l.game = g AND l.user = :user) |
||
| 441 | AND (g.startDate <= :date OR g.startDate IS NULL) |
||
| 442 | AND (g.endDate >= :date OR g.endDate IS NULL) |
||
| 443 | AND g.active = 1 AND g.broadcastPlatform = 1 |
||
| 444 | ORDER BY g.startDate ASC'); |
||
| 445 | $query->setParameter('date', $today); |
||
| 446 | $query->setParameter('user', $user); |
||
| 447 | $query->setMaxResults($maxResults); |
||
| 448 | $games = $query->getResult(); |
||
| 449 | |||
| 450 | return $games; |
||
| 451 | } |
||
| 452 | |||
| 453 | View Code Duplication | public function getEntriesQuery($game) |
|
| 454 | { |
||
| 455 | $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
||
| 456 | |||
| 457 | $qb = $em->createQueryBuilder(); |
||
| 458 | $qb->select(' |
||
| 459 | e.id, |
||
| 460 | u.username, |
||
| 461 | u.title, |
||
| 462 | u.firstname, |
||
| 463 | u.lastname, |
||
| 464 | u.email, |
||
| 465 | u.optin, |
||
| 466 | u.optinPartner, |
||
| 467 | u.address, |
||
| 468 | u.address2, |
||
| 469 | u.postalCode, |
||
| 470 | u.city, |
||
| 471 | u.country, |
||
| 472 | u.telephone, |
||
| 473 | u.mobile, |
||
| 474 | u.created_at, |
||
| 475 | u.dob, |
||
| 476 | e.winner, |
||
| 477 | e.socialShares, |
||
| 478 | e.playerData, |
||
| 479 | e.updated_at |
||
| 480 | ') |
||
| 481 | ->from('PlaygroundGame\Entity\Entry', 'e') |
||
| 482 | ->leftJoin('e.user', 'u') |
||
| 483 | ->where($qb->expr()->eq('e.game', ':game')); |
||
| 484 | |||
| 485 | $qb->setParameter('game', $game); |
||
| 486 | |||
| 487 | return $qb->getQuery(); |
||
| 488 | } |
||
| 489 | |||
| 490 | public function getEntriesHeader($game) |
||
| 491 | { |
||
| 492 | if ($game->getPlayerForm()) { |
||
| 493 | $formPV = json_decode($game->getPlayerForm()->getForm(), true); |
||
| 494 | $header = array('id'=> 1); |
||
| 495 | View Code Duplication | foreach ($formPV as $element) { |
|
| 496 | foreach ($element as $k => $v) { |
||
| 497 | if ($k !== 'form_properties') { |
||
| 498 | $header[$v[0]['name']] = 1; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | } |
||
| 502 | } else { |
||
| 503 | $header = array( |
||
| 504 | 'id' => 1, |
||
| 505 | 'username' => 1, |
||
| 506 | 'title' => 1, |
||
| 507 | 'firstname' => 1, |
||
| 508 | 'lastname' => 1, |
||
| 509 | 'email' => 1, |
||
| 510 | 'optin' => 1, |
||
| 511 | 'optinPartner' => 1, |
||
| 512 | 'address' => 1, |
||
| 513 | 'address2' => 1, |
||
| 514 | 'postalCode' => 1, |
||
| 515 | 'city' => 1, |
||
| 516 | 'country' => 1, |
||
| 517 | 'telephone' => 1, |
||
| 518 | 'mobile' => 1, |
||
| 519 | 'created_at' => 1, |
||
| 520 | 'dob' => 1, |
||
| 521 | 'winner' => 1 |
||
| 522 | ); |
||
| 523 | } |
||
| 524 | $header['winner'] = 1; |
||
| 525 | $header['socialShares'] = 1; |
||
| 526 | $header['updated_at'] = 1; |
||
| 527 | |||
| 528 | return $header; |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * getGameEntries : I create an array of entries based on playerData + header |
||
| 533 | * |
||
| 534 | * @return Array of PlaygroundGame\Entity\Game |
||
| 535 | */ |
||
| 536 | public function getGameEntries($header, $entries, $game) |
||
| 537 | { |
||
| 538 | $header = $this->getEntriesHeader($game); |
||
| 539 | |||
| 540 | $results = array(); |
||
| 541 | |||
| 542 | foreach ($entries as $k => $entry) { |
||
| 543 | $entryData = json_decode($entry['playerData'], true); |
||
| 544 | View Code Duplication | foreach ($header as $key => $v) { |
|
| 545 | if (isset($entryData[$key])) { |
||
| 546 | $results[$k][$key] = (is_array($entryData[$key]))?implode(', ', $entryData[$key]):$entryData[$key]; |
||
| 547 | } elseif (array_key_exists($key, $entry)) { |
||
| 548 | $results[$k][$key] = ($entry[$key] instanceof \DateTime)? |
||
| 549 | $entry[$key]->format('Y-m-d H:i:s'): |
||
| 550 | $entry[$key]; |
||
| 551 | } else { |
||
| 552 | $results[$k][$key] = ''; |
||
| 553 | } |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | return $results; |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * getActiveSliderGames |
||
| 562 | * |
||
| 563 | * @return Array of PlaygroundGame\Entity\Game |
||
| 564 | */ |
||
| 565 | public function getActiveSliderGames() |
||
| 566 | { |
||
| 567 | $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default'); |
||
| 568 | $today = new \DateTime("now"); |
||
| 569 | $today = $today->format('Y-m-d H:i:s'); |
||
| 570 | |||
| 571 | // Game active with a start_date before today (or without start_date) |
||
| 572 | // and end_date after today (or without end-date) |
||
| 573 | $query = $em->createQuery('SELECT g FROM PlaygroundGame\Entity\Game g |
||
| 574 | WHERE (g.publicationDate <= :date OR g.publicationDate IS NULL) |
||
| 575 | AND (g.closeDate >= :date OR g.closeDate IS NULL) |
||
| 576 | AND g.active = true AND g.broadcastPlatform = 1 AND g.pushHome = true'); |
||
| 577 | $query->setParameter('date', $today); |
||
| 578 | $games = $query->getResult(); |
||
| 579 | |||
| 580 | // je les classe par date de publication (date comme clé dans le tableau afin de pouvoir merger les objets |
||
| 581 | // de type article avec le même procédé en les classant naturellement par date asc ou desc |
||
| 582 | $arrayGames = array(); |
||
| 583 | View Code Duplication | foreach ($games as $game) { |
|
| 584 | if ($game->getPublicationDate()) { |
||
| 585 | $key = $game->getPublicationDate()->format('Ymd'); |
||
| 586 | } elseif ($game->getStartDate()) { |
||
| 587 | $key = $game->getStartDate()->format('Ymd'); |
||
| 588 | } else { |
||
| 589 | $key = $game->getUpdatedAt()->format('Ymd'); |
||
| 590 | } |
||
| 591 | $key .= $game->getUpdatedAt()->format('Ymd') . '-' . $game->getId(); |
||
| 592 | $arrayGames[$key] = $game; |
||
| 593 | } |
||
| 594 | |||
| 595 | return $arrayGames; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * getPrizeCategoryGames |
||
| 600 | * |
||
| 601 | * @return Array of PlaygroundGame\Entity\Game |
||
| 602 | */ |
||
| 603 | View Code Duplication | public function getPrizeCategoryGames($categoryid) |
|
| 615 | |||
| 616 | public function checkGame($identifier, $checkIfStarted = true) |
||
| 617 | { |
||
| 618 | $gameMapper = $this->getGameMapper(); |
||
| 619 | |||
| 620 | if (! $identifier) { |
||
| 621 | return false; |
||
| 622 | } |
||
| 623 | |||
| 624 | $game = $gameMapper->findByIdentifier($identifier); |
||
| 625 | |||
| 626 | // the game has not been found |
||
| 627 | if (! $game) { |
||
| 628 | return false; |
||
| 629 | } |
||
| 630 | |||
| 631 | // for preview stuff as admin |
||
| 632 | if ($this->isAllowed('game', 'edit')) { |
||
| 633 | $r =$this->serviceLocator->get('request'); |
||
| 634 | if ($r->getQuery()->get('preview')) { |
||
| 635 | $game->setActive(true); |
||
| 636 | $game->setStartDate(null); |
||
| 637 | $game->setEndDate(null); |
||
| 638 | $game->setPublicationDate(null); |
||
| 639 | $game->setBroadcastPlatform(true); |
||
| 640 | |||
| 641 | // I don't want the game to be updated through any update during the preview mode. |
||
| 642 | // I mark it as readonly for Doctrine |
||
| 643 | $this->serviceLocator |
||
| 644 | ->get('doctrine.entitymanager.orm_default') |
||
| 645 | ->getUnitOfWork() |
||
| 646 | ->markReadOnly($game); |
||
| 647 | |||
| 648 | return $game; |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | // The game is inactive |
||
| 653 | if (! $game->getActive()) { |
||
| 654 | return false; |
||
| 655 | } |
||
| 656 | |||
| 657 | // the game has not begun yet |
||
| 658 | if (! $game->isOpen()) { |
||
| 659 | return false; |
||
| 660 | } |
||
| 661 | |||
| 662 | // the game is finished and closed |
||
| 663 | if (! $game->isStarted() && $checkIfStarted) { |
||
| 664 | return false; |
||
| 665 | } |
||
| 666 | |||
| 667 | return $game; |
||
| 668 | } |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Return the last entry of the user on this game, if it exists. |
||
| 672 | * An entry can be associated to : |
||
| 673 | * - A user account (a real one, linked to PlaygroundUser |
||
| 674 | * - An anonymous Identifier (based on one value of playerData (generally email)) |
||
| 675 | * - A cookie set on the player device (the less secure) |
||
| 676 | * If the active param is set, it can check if the entry is active or not. |
||
| 677 | * If the bonus param is set, it can check if the entry is a bonus or not. |
||
| 678 | * |
||
| 679 | * @param unknown $game |
||
| 680 | * @param string $user |
||
| 681 | * @param boolean $active |
||
| 682 | * @param boolean $bonus |
||
| 683 | * @return boolean |
||
| 684 | */ |
||
| 685 | public function checkExistingEntry($game, $user = null, $active = null, $bonus = null) |
||
| 686 | { |
||
| 687 | $search = array('game' => $game); |
||
| 688 | |||
| 689 | if ($user) { |
||
| 690 | $search['user'] = $user; |
||
| 691 | } elseif ($this->getAnonymousIdentifier()) { |
||
| 692 | $search['anonymousIdentifier'] = $this->getAnonymousIdentifier(); |
||
| 693 | $search['user'] = null; |
||
| 694 | } else { |
||
| 695 | $search['anonymousId'] = $this->getAnonymousId(); |
||
| 696 | $search['user'] = null; |
||
| 697 | } |
||
| 698 | |||
| 699 | if (! is_null($active)) { |
||
| 700 | $search['active'] = $active; |
||
| 701 | } |
||
| 702 | if (! is_null($bonus)) { |
||
| 703 | $search['bonus'] = $bonus; |
||
| 704 | } |
||
| 705 | |||
| 706 | $entry = $this->getEntryMapper()->findOneBy($search, array('updated_at' => 'desc')); |
||
| 707 | |||
| 708 | return $entry; |
||
| 709 | } |
||
| 710 | |||
| 711 | /* |
||
| 712 | * This function updates the entry with the player data after checking |
||
| 713 | * that the data are compliant with the formUser Game attribute |
||
| 714 | * |
||
| 715 | * The $data has to be a json object |
||
| 716 | */ |
||
| 717 | public function updateEntryPlayerForm($data, $game, $user, $entry, $mandatory = true) |
||
| 718 | { |
||
| 719 | $form = $this->createFormFromJson($game->getPlayerForm()->getForm(), 'playerForm'); |
||
| 720 | $form->setData($data); |
||
| 721 | |||
| 722 | if (!$mandatory) { |
||
| 723 | $filter = $form->getInputFilter(); |
||
| 724 | foreach ($form->getElements() as $element) { |
||
| 725 | try { |
||
| 726 | $elementInput = $filter->get($element->getName()); |
||
| 727 | $elementInput->setRequired(false); |
||
| 728 | $form->get($element->getName())->setAttribute('required', false); |
||
| 729 | } catch (\Zend\Form\Exception\InvalidElementException $e) { |
||
| 730 | } |
||
| 731 | } |
||
| 732 | } |
||
| 733 | |||
| 734 | if ($form->isValid()) { |
||
| 735 | $dataJson = json_encode($form->getData()); |
||
| 736 | |||
| 737 | if ($game->getAnonymousAllowed() && |
||
| 738 | $game->getAnonymousIdentifier() && |
||
| 739 | isset($data[$game->getAnonymousIdentifier()]) |
||
| 740 | ) { |
||
| 741 | $session = new \Zend\Session\Container('anonymous_identifier'); |
||
| 742 | if (empty($session->offsetGet('anonymous_identifier'))) { |
||
| 743 | $anonymousIdentifier = $data[$game->getAnonymousIdentifier()]; |
||
| 744 | |||
| 745 | $entry->setAnonymousIdentifier($anonymousIdentifier); |
||
| 746 | |||
| 747 | // I must transmit this info during the whole game workflow |
||
| 748 | $session->offsetSet('anonymous_identifier', $anonymousIdentifier); |
||
| 749 | } |
||
| 750 | } |
||
| 751 | |||
| 752 | $entry->setPlayerData($dataJson); |
||
| 753 | $this->getEntryMapper()->update($entry); |
||
| 754 | } else { |
||
| 755 | return false; |
||
| 756 | } |
||
| 757 | |||
| 758 | return true; |
||
| 759 | } |
||
| 760 | |||
| 761 | |||
| 762 | public function checkIsFan($game) |
||
| 763 | { |
||
| 764 | // If on Facebook, check if you have to be a FB fan to play the game |
||
| 765 | $session = new Container('facebook'); |
||
| 766 | |||
| 767 | if ($session->offsetExists('signed_request')) { |
||
| 768 | // I'm on Facebook |
||
| 769 | $sr = $session->offsetGet('signed_request'); |
||
| 770 | if ($sr['page']['liked'] == 1) { |
||
| 771 | return true; |
||
| 772 | } |
||
| 773 | } else { |
||
| 774 | // I'm not on Facebook |
||
| 775 | return true; |
||
| 776 | } |
||
| 777 | |||
| 778 | return false; |
||
| 779 | } |
||
| 780 | |||
| 781 | public function getAnonymousIdentifier() |
||
| 782 | { |
||
| 783 | if (is_null($this->anonymousIdentifier)) { |
||
| 784 | // If on Facebook, check if you have to be a FB fan to play the game |
||
| 785 | $session = new Container('anonymous_identifier'); |
||
| 786 | |||
| 787 | if ($session->offsetExists('anonymous_identifier')) { |
||
| 788 | $this->anonymousIdentifier = $session->offsetGet('anonymous_identifier'); |
||
| 789 | } else { |
||
| 790 | $this->anonymousIdentifier = false; |
||
| 791 | } |
||
| 792 | } |
||
| 793 | |||
| 794 | return $this->anonymousIdentifier; |
||
| 795 | } |
||
| 796 | |||
| 797 | /** |
||
| 798 | * errors : |
||
| 799 | * -1 : user not connected |
||
| 800 | * -2 : limit entry games for this user reached |
||
| 801 | * |
||
| 802 | * @param \PlaygroundGame\Entity\Game $game |
||
| 803 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
| 804 | * @return number unknown |
||
| 805 | */ |
||
| 806 | public function play($game, $user) |
||
| 807 | { |
||
| 808 | |||
| 809 | // certaines participations peuvent rester ouvertes. |
||
| 810 | // On autorise alors le joueur à reprendre là ou il en était |
||
| 811 | // par exemple les postvote... |
||
| 812 | $entry = $this->checkExistingEntry($game, $user, true); |
||
| 813 | |||
| 814 | if (! $entry) { |
||
| 815 | if ($this->hasReachedPlayLimit($game, $user)) { |
||
| 816 | return false; |
||
| 817 | } |
||
| 818 | |||
| 819 | $entry = new Entry(); |
||
| 820 | $entry->setGame($game); |
||
| 821 | $entry->setUser($user); |
||
| 822 | $entry->setPoints(0); |
||
| 823 | $entry->setIp($this->getIp()); |
||
| 824 | $entry->setAnonymousId($this->getAnonymousId()); |
||
| 825 | if ($this->getAnonymousIdentifier()) { |
||
| 826 | $entry->setAnonymousIdentifier($this->getAnonymousIdentifier()); |
||
| 827 | } |
||
| 828 | |||
| 829 | $entry = $this->getEntryMapper()->insert($entry); |
||
| 830 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
| 831 | 'user' => $user, |
||
| 832 | 'game' => $game, |
||
| 833 | 'entry' => $entry |
||
| 834 | )); |
||
| 835 | } |
||
| 836 | |||
| 837 | return $entry; |
||
| 838 | } |
||
| 839 | |||
| 840 | /** |
||
| 841 | * @param \PlaygroundGame\Entity\Game $game |
||
| 842 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
| 843 | */ |
||
| 844 | public function hasReachedPlayLimit($game, $user) |
||
| 859 | |||
| 860 | /** |
||
| 861 | * @param \PlaygroundGame\Entity\Game $game |
||
| 862 | * @param \PlaygroundUser\Entity\UserInterface $user |
||
| 863 | */ |
||
| 864 | public function findLastEntries($game, $user, $limitScale) |
||
| 865 | { |
||
| 866 | $limitDate = $this->getLimitDate($limitScale); |
||
| 867 | |||
| 868 | if ($user) { |
||
| 869 | return $this->getEntryMapper()->findLastEntriesByUser($game, $user, $limitDate); |
||
| 870 | } elseif ($this->getAnonymousIdentifier()) { |
||
| 871 | return $this->getEntryMapper()->findLastEntriesByAnonymousIdentifier( |
||
| 872 | $game, |
||
| 873 | $this->getAnonymousIdentifier(), |
||
| 874 | $limitDate |
||
| 875 | ); |
||
| 876 | } else { |
||
| 877 | return $this->getEntryMapper()->findLastEntriesByIp($game, $this->getIp(), $limitDate); |
||
| 878 | } |
||
| 879 | } |
||
| 880 | |||
| 881 | /** |
||
| 882 | * |
||
| 883 | * |
||
| 884 | */ |
||
| 885 | public function getLimitDate($limitScale) |
||
| 886 | { |
||
| 887 | $now = new \DateTime("now"); |
||
| 888 | switch ($limitScale) { |
||
| 889 | case 'always': |
||
| 890 | $interval = 'P100Y'; |
||
| 891 | $now->sub(new \DateInterval($interval)); |
||
| 892 | $dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
||
| 893 | break; |
||
| 894 | case 'day': |
||
| 895 | $dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
||
| 896 | break; |
||
| 897 | View Code Duplication | case 'week': |
|
| 898 | $interval = 'P7D'; |
||
| 899 | $now->sub(new \DateInterval($interval)); |
||
| 900 | $dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
||
| 901 | break; |
||
| 902 | View Code Duplication | case 'month': |
|
| 903 | $interval = 'P1M'; |
||
| 904 | $now->sub(new \DateInterval($interval)); |
||
| 905 | $dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
||
| 906 | break; |
||
| 907 | View Code Duplication | case 'year': |
|
| 908 | $interval = 'P1Y'; |
||
| 909 | $now->sub(new \DateInterval($interval)); |
||
| 910 | $dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
||
| 911 | break; |
||
| 912 | View Code Duplication | default: |
|
| 913 | $interval = 'P100Y'; |
||
| 914 | $now->sub(new \DateInterval($interval)); |
||
| 915 | $dateLimit = $now->format('Y-m-d') . ' 0:0:0'; |
||
| 916 | } |
||
| 917 | |||
| 918 | return $dateLimit; |
||
| 919 | } |
||
| 920 | |||
| 921 | public function findLastActiveEntry($game, $user) |
||
| 922 | { |
||
| 923 | return $this->checkExistingEntry($game, $user, true); |
||
| 924 | } |
||
| 925 | |||
| 926 | public function findLastInactiveEntry($game, $user) |
||
| 927 | { |
||
| 928 | return $this->checkExistingEntry($game, $user, false, false); |
||
| 929 | } |
||
| 930 | |||
| 931 | public function findLastEntry($game, $user) |
||
| 932 | { |
||
| 933 | return $this->checkExistingEntry($game, $user, null, false); |
||
| 934 | } |
||
| 935 | |||
| 936 | public function inviteToTeam($data, $game, $user) |
||
| 999 | |||
| 1000 | public function sendShareMail( |
||
| 1001 | $data, |
||
| 1002 | $game, |
||
| 1003 | $user, |
||
| 1004 | $entry, |
||
| 1005 | $template = 'share_game', |
||
| 1006 | $topic = null, |
||
| 1007 | $userTimer = array(), |
||
| 1008 | $subject = '' |
||
| 1009 | ) { |
||
| 1010 | $mailService = $this->serviceLocator->get('playgroundgame_message'); |
||
| 1011 | $mailSent = false; |
||
| 1012 | $from = $this->getOptions()->getEmailFromAddress(); |
||
| 1013 | |||
| 1014 | if (empty($subject)) { |
||
| 1015 | $subject = $this->serviceLocator->get('translator')->translate( |
||
| 1016 | $this->getOptions()->getShareSubjectLine(), |
||
| 1017 | 'playgroundgame' |
||
| 1018 | ); |
||
| 1019 | } else { |
||
| 1020 | $subject = $this->serviceLocator->get('translator')->translate( |
||
| 1021 | $subject, |
||
| 1022 | 'playgroundgame' |
||
| 1023 | ); |
||
| 1024 | } |
||
| 1025 | |||
| 1026 | $renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); |
||
| 1027 | $skinUrl = $renderer->url( |
||
| 1028 | 'frontend', |
||
| 1029 | array(), |
||
| 1030 | array('force_canonical' => true) |
||
| 1031 | ); |
||
| 1032 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true) . '####' . time()), 0, 15)); |
||
| 1033 | |||
| 1034 | if (! $topic) { |
||
| 1035 | $topic = $game->getTitle(); |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | if (isset($data['email']) && !is_array($data['email'])) { |
||
| 1039 | $data['email'] = array($data['email']); |
||
| 1040 | } |
||
| 1041 | |||
| 1042 | foreach ($data['email'] as $to) { |
||
| 1043 | $mailSent = true; |
||
| 1044 | if (!empty($to)) { |
||
| 1045 | $message = $mailService->createHtmlMessage( |
||
| 1046 | $from, |
||
| 1047 | $to, |
||
| 1048 | $subject, |
||
| 1049 | 'playground-game/email/' . $template, |
||
| 1050 | array( |
||
| 1051 | 'game' => $game, |
||
| 1052 | 'data' => $data, |
||
| 1053 | 'from' => $from, |
||
| 1054 | 'to' => $to, |
||
| 1055 | 'secretKey' => $secretKey, |
||
| 1056 | 'skinUrl' => $skinUrl, |
||
| 1057 | 'userTimer' => $userTimer |
||
| 1058 | ) |
||
| 1059 | ); |
||
| 1060 | try { |
||
| 1061 | $mailService->send($message); |
||
| 1062 | } catch (\Zend\Mail\Protocol\Exception\RuntimeException $e) { |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | if ($entry) { |
||
| 1066 | $shares = json_decode($entry->getSocialShares(), true); |
||
| 1067 | (!isset($shares['mail']))? $shares['mail'] = 1:$shares['mail'] += 1; |
||
| 1068 | } |
||
| 1069 | } |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | if ($mailSent) { |
||
| 1073 | if ($entry) { |
||
| 1074 | $sharesJson = json_encode($shares); |
||
| 1075 | $entry->setSocialShares($sharesJson); |
||
| 1076 | $entry = $this->getEntryMapper()->update($entry); |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
| 1080 | 'user' => $user, |
||
| 1081 | 'topic' => $topic, |
||
| 1082 | 'secretKey' => $secretKey, |
||
| 1083 | 'data' => $data, |
||
| 1084 | 'game' => $game, |
||
| 1085 | 'entry' => $entry |
||
| 1086 | )); |
||
| 1087 | |||
| 1088 | return true; |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | return false; |
||
| 1092 | } |
||
| 1093 | |||
| 1094 | /** |
||
| 1095 | * @param \PlaygroundGame\Entity\Game $game |
||
| 1096 | * @param \PlaygroundUser\Entity\User $user |
||
| 1097 | * @param Entry $entry |
||
| 1098 | * @param \PlaygroundGame\Entity\Prize $prize |
||
| 1099 | */ |
||
| 1100 | public function sendResultMail($game, $user, $entry, $template = 'entry', $prize = null) |
||
| 1101 | { |
||
| 1102 | $mailService = $this->serviceLocator->get('playgroundgame_message'); |
||
| 1103 | $from = $this->getOptions()->getEmailFromAddress(); |
||
| 1104 | if ($user) { |
||
| 1105 | $to = $user->getEmail(); |
||
| 1106 | } elseif ($entry->getAnonymousIdentifier()) { |
||
| 1107 | $to = $entry->getAnonymousIdentifier(); |
||
| 1108 | } else { |
||
| 1109 | return false; |
||
| 1110 | } |
||
| 1111 | $subject = $game->getTitle(); |
||
| 1112 | $renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); |
||
| 1113 | $skinUrl = $renderer->url( |
||
| 1114 | 'frontend', |
||
| 1115 | array(), |
||
| 1116 | array('force_canonical' => true) |
||
| 1117 | ); |
||
| 1118 | $message = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/' . $template, array( |
||
| 1119 | 'game' => $game, |
||
| 1120 | 'entry' => $entry, |
||
| 1121 | 'skinUrl' => $skinUrl, |
||
| 1122 | 'prize' => $prize |
||
| 1123 | )); |
||
| 1124 | $mailService->send($message); |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | public function sendGameMail($game, $user, $post, $template = 'postvote') |
||
| 1128 | { |
||
| 1129 | $mailService = $this->serviceLocator->get('playgroundgame_message'); |
||
| 1130 | $from = $this->getOptions()->getEmailFromAddress(); |
||
| 1131 | $to = $user->getEmail(); |
||
| 1132 | $subject = $this->serviceLocator->get('translator')->translate( |
||
| 1133 | $this->getOptions()->getParticipationSubjectLine(), |
||
| 1134 | 'playgroundgame' |
||
| 1135 | ); |
||
| 1136 | $renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface'); |
||
| 1137 | $skinUrl = $renderer->url( |
||
| 1138 | 'frontend', |
||
| 1139 | array(), |
||
| 1140 | array('force_canonical' => true) |
||
| 1141 | ); |
||
| 1142 | |||
| 1143 | $message = $mailService->createHtmlMessage($from, $to, $subject, 'playground-game/email/' . $template, array( |
||
| 1144 | 'game' => $game, |
||
| 1145 | 'post' => $post, |
||
| 1146 | 'skinUrl' => $skinUrl |
||
| 1147 | )); |
||
| 1148 | $mailService->send($message); |
||
| 1149 | } |
||
| 1150 | |||
| 1151 | View Code Duplication | public function postFbWall($secretKey, $game, $user, $entry) |
|
| 1152 | { |
||
| 1153 | $topic = $game->getTitle(); |
||
| 1154 | |||
| 1155 | $shares = json_decode($entry->getSocialShares(), true); |
||
| 1156 | if (!isset($shares['fbwall'])) { |
||
| 1157 | $shares['fbwall'] = 1; |
||
| 1158 | } else { |
||
| 1159 | $shares['fbwall'] += 1; |
||
| 1160 | } |
||
| 1161 | $sharesJson = json_encode($shares); |
||
| 1162 | $entry->setSocialShares($sharesJson); |
||
| 1163 | $entry = $this->getEntryMapper()->update($entry); |
||
| 1164 | |||
| 1165 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
| 1166 | 'user' => $user, |
||
| 1167 | 'game' => $game, |
||
| 1168 | 'secretKey' => $secretKey, |
||
| 1169 | 'topic' => $topic, |
||
| 1170 | 'entry' => $entry |
||
| 1171 | )); |
||
| 1172 | |||
| 1173 | return true; |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | public function postFbRequest($secretKey, $game, $user, $entry, $to) |
||
| 1177 | { |
||
| 1178 | $shares = json_decode($entry->getSocialShares(), true); |
||
| 1179 | $to = explode(',', $to); |
||
| 1180 | if (!isset($shares['fbrequest'])) { |
||
| 1181 | $shares['fbrequest'] = count($to); |
||
| 1182 | } else { |
||
| 1183 | $shares['fbrequest'] += count($to); |
||
| 1184 | } |
||
| 1185 | $sharesJson = json_encode($shares); |
||
| 1186 | $entry->setSocialShares($sharesJson); |
||
| 1187 | $entry = $this->getEntryMapper()->update($entry); |
||
| 1188 | |||
| 1189 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
| 1190 | 'user' => $user, |
||
| 1191 | 'game' => $game, |
||
| 1192 | 'secretKey' => $secretKey, |
||
| 1193 | 'entry' => $entry, |
||
| 1194 | 'invites' => count($to) |
||
| 1195 | )); |
||
| 1196 | |||
| 1197 | return true; |
||
| 1198 | } |
||
| 1199 | |||
| 1200 | View Code Duplication | public function postTwitter($secretKey, $game, $user, $entry) |
|
| 1201 | { |
||
| 1202 | $topic = $game->getTitle(); |
||
| 1203 | |||
| 1204 | $shares = json_decode($entry->getSocialShares(), true); |
||
| 1205 | if (!isset($shares['fbrequest'])) { |
||
| 1206 | $shares['tweet'] = 1; |
||
| 1207 | } else { |
||
| 1208 | $shares['tweet'] += 1; |
||
| 1209 | } |
||
| 1210 | $sharesJson = json_encode($shares); |
||
| 1211 | $entry->setSocialShares($sharesJson); |
||
| 1212 | $entry = $this->getEntryMapper()->update($entry); |
||
| 1213 | |||
| 1214 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
| 1215 | 'user' => $user, |
||
| 1216 | 'game' => $game, |
||
| 1217 | 'secretKey' => $secretKey, |
||
| 1218 | 'topic' => $topic, |
||
| 1219 | 'entry' => $entry |
||
| 1220 | )); |
||
| 1221 | |||
| 1222 | return true; |
||
| 1223 | } |
||
| 1224 | |||
| 1225 | View Code Duplication | public function postGoogle($secretKey, $game, $user, $entry) |
|
| 1226 | { |
||
| 1227 | $topic = $game->getTitle(); |
||
| 1228 | |||
| 1229 | $shares = json_decode($entry->getSocialShares(), true); |
||
| 1230 | if (!isset($shares['fbrequest'])) { |
||
| 1231 | $shares['google'] = 1; |
||
| 1232 | } else { |
||
| 1233 | $shares['google'] += 1; |
||
| 1234 | } |
||
| 1235 | $sharesJson = json_encode($shares); |
||
| 1236 | $entry->setSocialShares($sharesJson); |
||
| 1237 | $entry = $this->getEntryMapper()->update($entry); |
||
| 1238 | |||
| 1239 | $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array( |
||
| 1240 | 'user' => $user, |
||
| 1241 | 'game' => $game, |
||
| 1242 | 'secretKey' => $secretKey, |
||
| 1243 | 'topic' => $topic, |
||
| 1244 | 'entry' => $entry |
||
| 1245 | )); |
||
| 1246 | |||
| 1247 | return true; |
||
| 1248 | } |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Is it possible to trigger a bonus entry ? |
||
| 1252 | * |
||
| 1253 | * @param unknown_type $game |
||
| 1254 | * @param unknown_type $user |
||
| 1255 | */ |
||
| 1256 | public function allowBonus($game, $user) |
||
| 1276 | |||
| 1277 | public function addAnotherEntry($game, $user, $winner = 0) |
||
| 1291 | |||
| 1292 | /** |
||
| 1293 | * This bonus entry doesn't give points nor badges |
||
| 1294 | * It's just there to increase the chances during the Draw |
||
| 1295 | * Old Name playBonus |
||
| 1296 | * |
||
| 1297 | * @param PlaygroundGame\Entity\Game $game |
||
| 1298 | * @param unknown $user |
||
| 1299 | * @return boolean unknown |
||
| 1300 | */ |
||
| 1301 | public function addAnotherChance($game, $user, $winner = 0) |
||
| 1311 | |||
| 1312 | /** |
||
| 1313 | * This bonus entry doesn't give points nor badges but can play again |
||
| 1314 | * |
||
| 1315 | * @param PlaygroundGame\Entity\Game $game |
||
| 1316 | * @param user $user |
||
| 1317 | * @return boolean unknown |
||
| 1318 | */ |
||
| 1319 | public function playAgain($game, $user, $winner = 0) |
||
| 1320 | { |
||
| 1321 | if ($this->allowBonus($game, $user)) { |
||
| 1322 | $entry = $this->addAnotherEntry($game, $user, $winner); |
||
| 1323 | $entry->setActive(1); |
||
| 1324 | $entry = $this->getEntryMapper()->update($entry); |
||
| 1325 | if ($entry->getActive() == 1) { |
||
| 1326 | return true; |
||
| 1327 | } |
||
| 1328 | } |
||
| 1329 | |||
| 1330 | return false; |
||
| 1331 | } |
||
| 1332 | |||
| 1333 | /** |
||
| 1334 | * @param string $path |
||
| 1335 | * @param boolean $noReplace : If the image name already exist, don't replace it but change the name |
||
| 1336 | */ |
||
| 1337 | public function uploadFile($path, $file, $noReplace = true) |
||
| 1338 | { |
||
| 1339 | $err = $file["error"]; |
||
| 1340 | $message = ''; |
||
| 1341 | if ($err > 0) { |
||
| 1342 | switch ($err) { |
||
| 1343 | case '1': |
||
| 1344 | $message .= 'Max file size exceeded. (php.ini)'; |
||
| 1345 | break; |
||
| 1346 | case '2': |
||
| 1347 | $message .= 'Max file size exceeded.'; |
||
| 1348 | break; |
||
| 1349 | case '3': |
||
| 1350 | $message .= 'File upload was only partial.'; |
||
| 1351 | break; |
||
| 1352 | case '4': |
||
| 1353 | $message .= 'No file was attached.'; |
||
| 1354 | break; |
||
| 1355 | case '7': |
||
| 1356 | $message .= 'File permission denied.'; |
||
| 1357 | break; |
||
| 1358 | default: |
||
| 1359 | $message .= 'Unexpected error occurs.'; |
||
| 1360 | } |
||
| 1361 | |||
| 1362 | return $err; |
||
| 1363 | } else { |
||
| 1364 | $fileNewname = $this->fileNewname($path, $file['name'], $noReplace); |
||
| 1365 | |||
| 1366 | if (isset($file["base64"])) { |
||
| 1367 | list(, $img) = explode(',', $file["base64"]); |
||
| 1368 | $img = str_replace(' ', '+', $img); |
||
| 1369 | $im = base64_decode($img); |
||
| 1370 | if ($im !== false) { |
||
| 1371 | // getimagesizefromstring |
||
| 1372 | file_put_contents($path . $fileNewname, $im); |
||
| 1373 | } else { |
||
| 1374 | return 1; |
||
| 1375 | } |
||
| 1376 | |||
| 1377 | return $fileNewname; |
||
| 1378 | } else { |
||
| 1379 | $adapter = new \Zend\File\Transfer\Adapter\Http(); |
||
| 1380 | // 1Mo |
||
| 1381 | $size = new Size(array( |
||
| 1382 | 'max' => 1024000 |
||
| 1383 | )); |
||
| 1384 | $is_image = new IsImage('jpeg,png,gif,jpg'); |
||
| 1385 | $adapter->setValidators(array( |
||
| 1386 | $size, |
||
| 1387 | $is_image |
||
| 1388 | ), $fileNewname); |
||
| 1389 | |||
| 1390 | if (! $adapter->isValid()) { |
||
| 1391 | return false; |
||
| 1392 | } |
||
| 1393 | |||
| 1394 | @move_uploaded_file($file["tmp_name"], $path . $fileNewname); |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | |||
| 1398 | if (class_exists("Imagick")) { |
||
| 1399 | $ext = pathinfo($fileNewname, PATHINFO_EXTENSION); |
||
| 1400 | $img = new \Imagick($path . $fileNewname); |
||
| 1401 | $img->cropThumbnailImage(100, 100); |
||
| 1402 | $img->setImageCompression(\Imagick::COMPRESSION_JPEG); |
||
| 1403 | $img->setImageCompressionQuality(75); |
||
| 1404 | // Strip out unneeded meta data |
||
| 1405 | $img->stripImage(); |
||
| 1406 | $img->writeImage($path . str_replace('.'.$ext, '-thumbnail.'.$ext, $fileNewname)); |
||
| 1407 | ErrorHandler::stop(true); |
||
| 1408 | } |
||
| 1409 | } |
||
| 1410 | |||
| 1411 | return $fileNewname; |
||
| 1412 | } |
||
| 1413 | |||
| 1414 | /** |
||
| 1415 | * @param string $path |
||
| 1416 | */ |
||
| 1417 | public function fileNewname($path, $filename, $generate = false) |
||
| 1418 | { |
||
| 1419 | $sanitize = new Sanitize(); |
||
| 1420 | $name = $sanitize->filter($filename); |
||
| 1421 | $newpath = $path . $name; |
||
| 1422 | |||
| 1423 | if ($generate) { |
||
| 1424 | if (file_exists($newpath)) { |
||
| 1425 | $filename = pathinfo($name, PATHINFO_FILENAME); |
||
| 1426 | $ext = pathinfo($name, PATHINFO_EXTENSION); |
||
| 1427 | |||
| 1428 | $name = $filename . '_' . rand(0, 99) . '.' . $ext; |
||
| 1429 | } |
||
| 1430 | } |
||
| 1431 | |||
| 1432 | unset($sanitize); |
||
| 1433 | |||
| 1434 | return $name; |
||
| 1435 | } |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * This function returns the list of games, order by $type |
||
| 1439 | */ |
||
| 1440 | public function getQueryGamesOrderBy($type = 'createdAt', $order = 'DESC') |
||
| 1488 | |||
| 1489 | public function draw($game) |
||
| 1511 | |||
| 1512 | /** |
||
| 1513 | * getGameMapper |
||
| 1514 | * |
||
| 1515 | * @return GameMapperInterface |
||
| 1516 | */ |
||
| 1517 | View Code Duplication | public function getGameMapper() |
|
| 1525 | |||
| 1526 | /** |
||
| 1527 | * setGameMapper |
||
| 1528 | * |
||
| 1529 | * @param GameMapperInterface $gameMapper |
||
| 1530 | * @return Game |
||
| 1531 | */ |
||
| 1532 | public function setGameMapper(GameMapperInterface $gameMapper) |
||
| 1538 | |||
| 1539 | /** |
||
| 1540 | * getEntryMapper |
||
| 1541 | * |
||
| 1542 | * @return EntryMapperInterface |
||
| 1543 | */ |
||
| 1544 | public function getEntryMapper() |
||
| 1552 | |||
| 1553 | /** |
||
| 1554 | * setEntryMapper |
||
| 1555 | * |
||
| 1556 | * @param EntryMapperInterface $entryMapper |
||
| 1557 | * @return Game |
||
| 1558 | */ |
||
| 1559 | public function setEntryMapper($entryMapper) |
||
| 1565 | |||
| 1566 | public function setOptions(ModuleOptions $options) |
||
| 1572 | |||
| 1573 | public function getOptions() |
||
| 1582 | |||
| 1583 | /** |
||
| 1584 | * @param string $str |
||
| 1585 | */ |
||
| 1586 | public function getExtension($str) |
||
| 1595 | |||
| 1596 | /** |
||
| 1597 | * @param string $extension |
||
| 1598 | */ |
||
| 1599 | public function getSrc($extension, $temp_path) |
||
| 1619 | |||
| 1620 | /** |
||
| 1621 | * @param string $extension |
||
| 1622 | * @param string $rep |
||
| 1623 | * @param integer $mini_width |
||
| 1624 | * @param integer $mini_height |
||
| 1625 | */ |
||
| 1626 | public function resize($tmp_file, $extension, $rep, $src, $mini_width, $mini_height) |
||
| 1627 | { |
||
| 1628 | list($src_width, $src_height) = getimagesize($tmp_file); |
||
| 1629 | |||
| 1630 | $ratio_src = $src_width / $src_height; |
||
| 1631 | $ratio_mini = $mini_width / $mini_height; |
||
| 1632 | |||
| 1633 | if ($ratio_src >= $ratio_mini) { |
||
| 1634 | $new_height_mini = $mini_height; |
||
| 1635 | $new_width_mini = $src_width / ($src_height / $mini_height); |
||
| 1636 | } else { |
||
| 1637 | $new_width_mini = $mini_width; |
||
| 1638 | $new_height_mini = $src_height / ($src_width / $mini_width); |
||
| 1639 | } |
||
| 1640 | |||
| 1641 | $new_image_mini = imagecreatetruecolor($mini_width, $mini_height); |
||
| 1642 | |||
| 1643 | imagecopyresampled( |
||
| 1644 | $new_image_mini, |
||
| 1645 | $src, |
||
| 1646 | 0 - ($new_width_mini - $mini_width) / 2, |
||
| 1647 | 0 - ($new_height_mini - $mini_height) / 2, |
||
| 1648 | 0, |
||
| 1649 | 0, |
||
| 1650 | $new_width_mini, |
||
| 1651 | $new_height_mini, |
||
| 1652 | $src_width, |
||
| 1653 | $src_height |
||
| 1654 | ); |
||
| 1655 | imagejpeg($new_image_mini, $rep); |
||
| 1656 | |||
| 1657 | imagedestroy($new_image_mini); |
||
| 1658 | } |
||
| 1659 | |||
| 1660 | public function getGameEntity() |
||
| 1664 | |||
| 1665 | /** |
||
| 1666 | * @param string $resource |
||
| 1667 | * @param string $privilege |
||
| 1668 | */ |
||
| 1669 | public function isAllowed($resource, $privilege = null) |
||
| 1675 | |||
| 1676 | public function getIp() |
||
| 1677 | { |
||
| 1678 | $ipaddress = ''; |
||
| 1679 | if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
||
| 1680 | $ipaddress = $_SERVER['HTTP_CLIENT_IP']; |
||
| 1681 | } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
||
| 1682 | $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; |
||
| 1683 | } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) { |
||
| 1684 | $ipaddress = $_SERVER['HTTP_X_FORWARDED']; |
||
| 1685 | } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) { |
||
| 1686 | $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; |
||
| 1687 | } elseif (isset($_SERVER['HTTP_FORWARDED'])) { |
||
| 1688 | $ipaddress = $_SERVER['HTTP_FORWARDED']; |
||
| 1689 | } elseif (isset($_SERVER['REMOTE_ADDR'])) { |
||
| 1690 | $ipaddress = $_SERVER['REMOTE_ADDR']; |
||
| 1691 | } else { |
||
| 1692 | $ipaddress = 'UNKNOWN'; |
||
| 1693 | } |
||
| 1694 | |||
| 1695 | return $ipaddress; |
||
| 1696 | } |
||
| 1697 | |||
| 1698 | public function getAnonymousId() |
||
| 1699 | { |
||
| 1700 | $anonymousId = ''; |
||
| 1701 | if ($_COOKIE && array_key_exists('pg_anonymous', $_COOKIE)) { |
||
| 1702 | $anonymousId = $_COOKIE['pg_anonymous']; |
||
| 1703 | } |
||
| 1704 | |||
| 1705 | return $anonymousId; |
||
| 1706 | } |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * |
||
| 1710 | * |
||
| 1711 | * This service is ready for all types of games |
||
| 1712 | * |
||
| 1713 | * @param array $data |
||
| 1714 | * @return \PlaygroundGame\Entity\Game |
||
| 1715 | */ |
||
| 1716 | View Code Duplication | public function createForm(array $data, $game, $form = null) |
|
| 1717 | { |
||
| 1718 | $title = ''; |
||
| 1719 | $description = ''; |
||
| 1720 | |||
| 1721 | if ($data['form_jsonified']) { |
||
| 1722 | $jsonPV = json_decode($data['form_jsonified']); |
||
| 1723 | foreach ($jsonPV as $element) { |
||
| 1724 | if ($element->form_properties) { |
||
| 1725 | $attributes = $element->form_properties[0]; |
||
| 1726 | $title = $attributes->title; |
||
| 1727 | $description = $attributes->description; |
||
| 1728 | |||
| 1729 | break; |
||
| 1730 | } |
||
| 1731 | } |
||
| 1732 | } |
||
| 1733 | if (! $form) { |
||
| 1734 | $form = new \PlaygroundGame\Entity\PlayerForm(); |
||
| 1735 | } |
||
| 1736 | $form->setGame($game); |
||
| 1737 | $form->setTitle($title); |
||
| 1738 | $form->setDescription($description); |
||
| 1739 | $form->setForm($data['form_jsonified']); |
||
| 1740 | $form->setFormTemplate($data['form_template']); |
||
| 1741 | |||
| 1742 | $form = $this->getPlayerFormMapper()->insert($form); |
||
| 1743 | |||
| 1744 | return $form; |
||
| 1745 | } |
||
| 1746 | |||
| 1747 | /** |
||
| 1748 | * getCSV creates lines of CSV and returns it. |
||
| 1749 | */ |
||
| 1750 | public function getCSV($array) |
||
| 1751 | { |
||
| 1752 | ob_start(); // buffer the output ... |
||
| 1753 | $out = fopen('php://output', 'w'); |
||
| 1754 | fputcsv($out, array_keys($array[0]), ";"); |
||
| 1755 | foreach ($array as $line) { |
||
| 1756 | fputcsv($out, $line, ";"); |
||
| 1757 | } |
||
| 1758 | fclose($out); |
||
| 1759 | return ob_get_clean(); // ... then return it as a string! |
||
| 1760 | } |
||
| 1761 | |||
| 1762 | public function getAttributes($attributes) |
||
| 1786 | |||
| 1787 | /** |
||
| 1788 | * @param \Zend\InputFilter\InputFilter $inputFilter |
||
| 1789 | */ |
||
| 1790 | public function decorate($element, $attr, $inputFilter) |
||
| 1853 | /** |
||
| 1854 | * Create a ZF2 Form from json data |
||
| 1855 | * @return Form |
||
| 1856 | */ |
||
| 1857 | public function createFormFromJson($jsonForm, $id = 'jsonForm') |
||
| 1858 | { |
||
| 1859 | $formPV = json_decode($jsonForm); |
||
| 1860 | |||
| 1861 | $form = new Form(); |
||
| 1862 | $form->setAttribute('id', $id); |
||
| 1863 | $form->setAttribute('enctype', 'multipart/form-data'); |
||
| 1864 | |||
| 1865 | $inputFilter = new \Zend\InputFilter\InputFilter(); |
||
| 1866 | $factory = new InputFactory(); |
||
| 1867 | |||
| 1868 | foreach ($formPV as $element) { |
||
| 1869 | View Code Duplication | if (isset($element->line_text)) { |
|
| 1870 | $attr = $this->getAttributes($element->line_text[0]); |
||
| 1871 | $element = new Element\Text($attr['name']); |
||
| 1872 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1873 | $form->add($element); |
||
| 1874 | } |
||
| 1875 | View Code Duplication | if (isset($element->line_password)) { |
|
| 1876 | $attr = $this->getAttributes($element->line_password[0]); |
||
| 1877 | $element = new Element\Password($attr['name']); |
||
| 1878 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1879 | $form->add($element); |
||
| 1880 | } |
||
| 1881 | View Code Duplication | if (isset($element->line_hidden)) { |
|
| 1882 | $attr = $this->getAttributes($element->line_hidden[0]); |
||
| 1883 | $element = new Element\Hidden($attr['name']); |
||
| 1884 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1885 | $form->add($element); |
||
| 1886 | } |
||
| 1887 | View Code Duplication | if (isset($element->line_email)) { |
|
| 1888 | $attr = $this->getAttributes($element->line_email[0]); |
||
| 1889 | $element = new Element\Email($attr['name']); |
||
| 1890 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1891 | $form->add($element); |
||
| 1892 | } |
||
| 1893 | View Code Duplication | if (isset($element->line_radio)) { |
|
| 1894 | $attr = $this->getAttributes($element->line_radio[0]); |
||
| 1895 | $element = new Element\Radio($attr['name']); |
||
| 1896 | |||
| 1897 | $element->setLabel($attr['label']); |
||
| 1898 | $element->setAttributes( |
||
| 1899 | array( |
||
| 1900 | 'name' => $attr['name'], |
||
| 1901 | 'required' => $attr['required'], |
||
| 1902 | 'allowEmpty'=> !$attr['required'], |
||
| 1903 | 'class' => $attr['class'], |
||
| 1904 | 'id' => $attr['id'] |
||
| 1905 | ) |
||
| 1906 | ); |
||
| 1907 | $values = array(); |
||
| 1908 | foreach ($attr['innerData'] as $value) { |
||
| 1909 | $values[] = $value->label; |
||
| 1910 | } |
||
| 1911 | $element->setValueOptions($values); |
||
| 1912 | |||
| 1913 | $options = array(); |
||
| 1914 | $options['encoding'] = 'UTF-8'; |
||
| 1915 | $options['disable_inarray_validator'] = true; |
||
| 1916 | |||
| 1917 | $element->setOptions($options); |
||
| 1918 | |||
| 1919 | $form->add($element); |
||
| 1920 | |||
| 1921 | $inputFilter->add($factory->createInput(array( |
||
| 1922 | 'name' => $attr['name'], |
||
| 1923 | 'required' => $attr['required'], |
||
| 1924 | 'allowEmpty' => !$attr['required'], |
||
| 1925 | ))); |
||
| 1926 | } |
||
| 1927 | View Code Duplication | if (isset($element->line_checkbox)) { |
|
| 1928 | $attr = $this->getAttributes($element->line_checkbox[0]); |
||
| 1929 | $element = new Element\MultiCheckbox($attr['name']); |
||
| 1930 | |||
| 1931 | $element->setLabel($attr['label']); |
||
| 1932 | $element->setAttributes( |
||
| 1933 | array( |
||
| 1934 | 'name' => $attr['name'], |
||
| 1935 | 'required' => $attr['required'], |
||
| 1936 | 'allowEmpty'=> !$attr['required'], |
||
| 1937 | 'class' => $attr['class'], |
||
| 1938 | 'id' => $attr['id'] |
||
| 1939 | ) |
||
| 1940 | ); |
||
| 1941 | |||
| 1942 | $values = array(); |
||
| 1943 | foreach ($attr['innerData'] as $value) { |
||
| 1944 | $values[] = $value->label; |
||
| 1945 | } |
||
| 1946 | $element->setValueOptions($values); |
||
| 1947 | $form->add($element); |
||
| 1948 | |||
| 1949 | $options = array(); |
||
| 1950 | $options['encoding'] = 'UTF-8'; |
||
| 1951 | $options['disable_inarray_validator'] = true; |
||
| 1952 | |||
| 1953 | $element->setOptions($options); |
||
| 1954 | |||
| 1955 | $inputFilter->add($factory->createInput(array( |
||
| 1956 | 'name' => $attr['name'], |
||
| 1957 | 'required' => $attr['required'], |
||
| 1958 | 'allowEmpty'=> !$attr['required'], |
||
| 1959 | ))); |
||
| 1960 | } |
||
| 1961 | View Code Duplication | if (isset($element->line_dropdown)) { |
|
| 1962 | $attr = $this->getAttributes($element->line_dropdown[0]); |
||
| 1963 | $element = new Element\Select($attr['name']); |
||
| 1964 | |||
| 1965 | $element->setLabel($attr['label']); |
||
| 1966 | $element->setAttributes( |
||
| 1967 | array( |
||
| 1968 | 'name' => $attr['name'], |
||
| 1969 | 'required' => $attr['required'], |
||
| 1970 | 'allowEmpty'=> !$attr['required'], |
||
| 1971 | 'class' => $attr['class'], |
||
| 1972 | 'id' => $attr['id'] |
||
| 1973 | ) |
||
| 1974 | ); |
||
| 1975 | $values = array(); |
||
| 1976 | foreach ($attr['dropdownValues'] as $value) { |
||
| 1977 | $values[] = $value->dropdown_label; |
||
| 1978 | } |
||
| 1979 | $element->setValueOptions($values); |
||
| 1980 | $form->add($element); |
||
| 1981 | |||
| 1982 | $options = array(); |
||
| 1983 | $options['encoding'] = 'UTF-8'; |
||
| 1984 | $options['disable_inarray_validator'] = true; |
||
| 1985 | |||
| 1986 | $element->setOptions($options); |
||
| 1987 | |||
| 1988 | $inputFilter->add($factory->createInput(array( |
||
| 1989 | 'name' => $attr['name'], |
||
| 1990 | 'required' => $attr['required'], |
||
| 1991 | 'allowEmpty' => !$attr['required'], |
||
| 1992 | ))); |
||
| 1993 | } |
||
| 1994 | View Code Duplication | if (isset($element->line_paragraph)) { |
|
| 1995 | $attr = $this->getAttributes($element->line_paragraph[0]); |
||
| 1996 | $element = new Element\Textarea($attr['name']); |
||
| 1997 | $element = $this->decorate($element, $attr, $inputFilter); |
||
| 1998 | $form->add($element); |
||
| 1999 | } |
||
| 2000 | if (isset($element->line_upload)) { |
||
| 2001 | $attr = $this->getAttributes($element->line_upload[0]); |
||
| 2002 | $element = new Element\File($attr['name']); |
||
| 2003 | |||
| 2004 | $element->setLabel($attr['label']); |
||
| 2005 | $element->setAttributes( |
||
| 2006 | array( |
||
| 2007 | 'name' => $attr['name'], |
||
| 2008 | 'required' => $attr['required'], |
||
| 2009 | 'class' => $attr['class'], |
||
| 2010 | 'id' => $attr['id'] |
||
| 2011 | ) |
||
| 2012 | ); |
||
| 2013 | $form->add($element); |
||
| 2014 | |||
| 2015 | $inputFilter->add($factory->createInput(array( |
||
| 2016 | 'name' => $attr['name'], |
||
| 2017 | 'required' => $attr['required'], |
||
| 2018 | 'validators' => array( |
||
| 2019 | array( |
||
| 2020 | 'name' => '\Zend\Validator\File\Size', |
||
| 2021 | 'options' => array('min' => $attr['filesizeMin'], 'max' => $attr['filesizeMax']) |
||
| 2022 | ), |
||
| 2023 | array( |
||
| 2024 | 'name' => '\Zend\Validator\File\Extension', |
||
| 2025 | 'options' => array( |
||
| 2026 | 'png,PNG,jpg,JPG,jpeg,JPEG,gif,GIF', |
||
| 2027 | 'messages' => array( |
||
| 2028 | \Zend\Validator\File\Extension::FALSE_EXTENSION =>'Veuillez télécharger une image' |
||
| 2029 | ) |
||
| 2030 | ) |
||
| 2031 | ), |
||
| 2032 | ), |
||
| 2033 | ))); |
||
| 2034 | } |
||
| 2035 | } |
||
| 2036 | |||
| 2037 | $form->setInputFilter($inputFilter); |
||
| 2038 | |||
| 2039 | return $form; |
||
| 2040 | } |
||
| 2041 | |||
| 2042 | /** |
||
| 2043 | * Send mail for winner and/or loser |
||
| 2044 | * @param \PlaygroundGame\Entity\Game $game |
||
| 2045 | * @param \PlaygroundUser\Entity\User $user |
||
| 2046 | * @param \PlaygroundGame\Entity\Entry $lastEntry |
||
| 2047 | * @param \PlaygroundGame\Entity\Prize $prize |
||
| 2048 | */ |
||
| 2049 | public function sendMail($game, $user, $lastEntry, $prize = null) |
||
| 2050 | { |
||
| 2051 | View Code Duplication | if (($user || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) && |
|
| 2052 | $game->getMailWinner() && |
||
| 2053 | $lastEntry->getWinner() |
||
| 2054 | ) { |
||
| 2055 | $this->sendResultMail($game, $user, $lastEntry, 'winner', $prize); |
||
| 2056 | } |
||
| 2057 | |||
| 2058 | View Code Duplication | if (($user || ($game->getAnonymousAllowed() && $game->getAnonymousIdentifier())) && |
|
| 2059 | $game->getMailLooser() && |
||
| 2060 | !$lastEntry->getWinner() |
||
| 2061 | ) { |
||
| 2062 | $this->sendResultMail($game, $user, $lastEntry, 'looser'); |
||
| 2063 | } |
||
| 2064 | } |
||
| 2065 | |||
| 2066 | public function getPlayerFormMapper() |
||
| 2067 | { |
||
| 2068 | if (null === $this->playerformMapper) { |
||
| 2069 | $this->playerformMapper = $this->serviceLocator->get('playgroundgame_playerform_mapper'); |
||
| 2070 | } |
||
| 2071 | |||
| 2072 | return $this->playerformMapper; |
||
| 2073 | } |
||
| 2074 | |||
| 2075 | public function setPlayerFormMapper($playerformMapper) |
||
| 2076 | { |
||
| 2077 | $this->playerformMapper = $playerformMapper; |
||
| 2078 | |||
| 2079 | return $this; |
||
| 2080 | } |
||
| 2081 | |||
| 2082 | public function getInvitationMapper() |
||
| 2090 | |||
| 2091 | public function setInvitationMapper($invitationMapper) |
||
| 2097 | |||
| 2098 | /** |
||
| 2099 | * getUserMapper |
||
| 2100 | * |
||
| 2101 | * @return UserMapperInterface |
||
| 2102 | */ |
||
| 2103 | public function getUserMapper() |
||
| 2111 | |||
| 2112 | /** |
||
| 2113 | * getUserMapper |
||
| 2114 | * |
||
| 2115 | * @return ServiceManager |
||
| 2116 | */ |
||
| 2117 | public function getServiceManager() |
||
| 2121 | } |
||
| 2122 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.