Completed
Branch master (30754b)
by Nikola
04:38 queued 02:12
created

ListController::getRates()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
crap 3.072
1
<?php
2
3
namespace RunOpenCode\Bundle\ExchangeRate\Controller;
4
5
use RunOpenCode\Bundle\ExchangeRate\Form\FilterType;
6
use RunOpenCode\Bundle\ExchangeRate\Security\AccessVoter;
7
use RunOpenCode\ExchangeRate\Contract\RateInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\Form\Form;
10
use Symfony\Component\HttpFoundation\Request;
11
12
/**
13
 * Class ListController
14
 *
15
 * @package RunOpenCode\Bundle\ExchangeRate\Controller
16
 */
17
class ListController extends Controller
18
{
19 2
    public function indexAction(Request $request)
20
    {
21 2
        if (!$this->isGranted(AccessVoter::VIEW, RateInterface::class)) {
22 1
            throw $this->createAccessDeniedException();
23
        }
24
25 1
        $filterForm = $this->getFilterForm($request);
26
27 1
        return $this->render('@ExchangeRate/list.html.twig', [
28 1
            'rates' => $this->getRates($filterForm),
29 1
            'form' => $filterForm->createView(),
30
        ]);
31
    }
32
33
    /**
34
     * Get rates for list view. Process filters if submitted.
35
     *
36
     * @param Form $filterForm
37
     *
38
     * @return \RunOpenCode\ExchangeRate\Contract\RateInterface[]
39
     */
40 1
    protected function getRates(Form $filterForm)
41
    {
42
        /**
43
         * @var \RunOpenCode\ExchangeRate\Contract\RepositoryInterface $repository
44
         */
45 1
        $repository = $this->get('runopencode.exchange_rate.repository');
46
47 1
        if ($filterForm->isSubmitted() && $filterForm->isValid()) {
48
            return $repository->all($filterForm->getData());
49
        }
50
51 1
        return $repository->all();
52
    }
53
54
    /**
55
     * Get filter form
56
     *
57
     * @param Request $request
58
     *
59
     * @return Form
60
     */
61 1
    protected function getFilterForm(Request $request)
62
    {
63 1
        $filter = $this->createForm($this->getFilterFormType());
64
65 1
        $filter->handleRequest($request);
66
67 1
        return $filter;
68
    }
69
70
    /**
71
     * Get FQCN of FilterType form.
72
     *
73
     * @return string
74
     */
75 1
    protected function getFilterFormType()
76
    {
77 1
        return FilterType::class;
78
    }
79
}
80