SeasonsController::getSeasonPerformances()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 10
Ratio 52.63 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 10
loc 19
ccs 0
cts 11
cp 0
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Entity\RepertoireSeason;
6
use App\Entity\Performance;
7
use App\Repository\RepertoireSeasonRepository;
8
use FOS\RestBundle\Controller\Annotations as Rest;
9
use FOS\RestBundle\Request\ParamFetcher;
10
use JMS\Serializer\SerializerInterface;
11
use Nelmio\ApiDocBundle\Annotation\Model;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
14
use Swagger\Annotations as SWG;
15
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
/**
19
 * @Route("/api/seasons")
20
 */
21
class SeasonsController extends AbstractController
22
{
23
    protected $seasonRepository;
24
    protected $serializer;
25
26
    public function __construct(RepertoireSeasonRepository $seasonRepository, SerializerInterface $serializer)
27
    {
28
        $this->seasonRepository = $seasonRepository;
29
        $this->serializer = $serializer;
30
    }
31
32
    /**
33
     * @Route("", name="get_seasons", methods={"GET"})
34
     * @SWG\Response(
35
     *     response=200,
36
     *     description="Returns when all parameters were correct",
37
     *     @SWG\Schema(
38
     *         type="array",
39
     *         @SWG\Items(ref=@Model(type=RepertoireSeason::class))
40
     *     )
41
     * )
42
     */
43
    public function index()
44
    {
45
        $seasons = $this->seasonRepository->findAllNotEmpty();
46
        return $seasons;
47
    }
48
49
    /**
50
     * @Route("/{number}/performances", name="get_season_performances", methods={"GET"})
51
     * @Entity("season", expr="repository.findOneByNumber(number)")
52
     * @SWG\Parameter(
53
     *     name="number",
54
     *     in="path",
55
     *     type="number",
56
     *     description="Season number. -1 for all performances for all seasons, 0 for most recent season"
57
     * )
58
     * @SWG\Response(
59
     *     response=200,
60
     *     description="Returns performances for choosen season",
61
     *     @SWG\Schema(
62
     *         type="array",
63
     *         @SWG\Items(ref=@Model(type=Performance::class))
64
     *     )
65
     * )
66
     * @SWG\Response(
67
     *     response=404,
68
     *     description="Returns when season number does not exists",
69
     * )
70
     * @Rest\QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
71
     */
72
    public function getSeasonPerformances(RepertoireSeason $season, ParamFetcher $paramFetcher)
73
    {
74
        $performances = $season->getPerformances()->toArray();
75
        $em = $this->getDoctrine()->getManager();
76
        $performancesTranslated = [];
77
78 View Code Duplication
        foreach ($performances as $performance) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
79
            $performance->setLocale($paramFetcher->get('locale'));
80
            $em->refresh($performance);
81
82
            if ($performance->getTranslations()) {
83
                $performance->unsetTranslations();
84
            }
85
86
            $performancesTranslated[] = $performance;
87
        }
88
89
        return $performancesTranslated;
90
    }
91
}
92