Completed
Push — master ( f46316...620406 )
by Nikola
07:51
created

ExchangeRateController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6667
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace RunOpenCode\Bundle\ExchangeRate\Controller;
4
5
use RunOpenCode\Bundle\ExchangeRate\Form\Type\NewType;
6
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface;
7
use RunOpenCode\Bundle\ExchangeRate\Model\Rate;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12
class ExchangeRateController extends Controller
13
{
14
    protected $repository;
15
16
    protected $baseCurrency;
17
18
    protected $settings;
19
20
    public function __construct(RepositoryInterface $repository, $baseCurrency, $settings)
21
    {
22
        $this->repository = $repository;
23
        $this->baseCurrency = $baseCurrency;
24
        $this->settings = $settings;
25
    }
26
27
    public function indexAction()
28
    {
29
        return $this->render($this->settings['list'], array(
30
            'base_template' => $this->settings['base_template'],
31
            'rates' => $this->repository->all(),
32
            'date_format' => $this->settings['date_format'],
33
            'date_time_format' => $this->settings['date_time_format']
34
        ));
35
    }
36
37 View Code Duplication
    public function newAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
38
    {
39
        $form = $this->createForm(NewType::class, $this->getNewRate());
40
41
        $form->handleRequest($request);
42
43
        if ($form->isSubmitted() && $form->isValid()) {
44
45
            $this->repository->save(array(
46
                $form->getData()
47
            ));
48
49
            $this->get('session')->getFlashBag()->add('success', 'run_open_code.exchange_rate.flash.new.success');
50
            return $this->redirectToRoute('roc_exchange_rate_list');
51
        }
52
53
        return $this->render($this->settings['new'], array(
54
            'base_template' => $this->settings['base_template'],
55
            'form' => $form->createView()
56
        ));
57
    }
58
59 View Code Duplication
    public function editAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
60
    {
61
        $form = $this->createForm(NewType::class, $this->getRateFromRequest($request));
62
63
        $form->handleRequest($request);
64
65
        if ($form->isSubmitted() && $form->isValid()) {
66
67
            $this->repository->save(array(
68
                $form->getData()
69
            ));
70
71
            $this->get('session')->getFlashBag()->add('success', 'run_open_code.exchange_rate.flash.edit.success');
72
            return $this->redirectToRoute('roc_exchange_rate_list');
73
        }
74
75
        return $this->render($this->settings['edit'], array(
76
            'base_template' => $this->settings['base_template'],
77
            'form' => $form->createView()
78
        ));
79
    }
80
81
    protected function getNewRate()
82
    {
83
        $rate = new Rate(null, null, $this->baseCurrency, null, null, $this->baseCurrency, null, null);
84
        $rate->setBaseCurrencyCode($this->baseCurrency);
85
86
        return $rate;
87
    }
88
89
    protected function getRateFromRequest(Request $request)
90
    {
91
        if (!$this->repository->has($request->get('currency_code'), \DateTime::createFromFormat('Y-m-d', $request->get('date')), $request->get('rate_type'))) {
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor... $request->get('date')) targeting DateTime::createFromFormat() can also be of type false; however, RunOpenCode\ExchangeRate...ositoryInterface::has() does only seem to accept null|object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
92
            throw new NotFoundHttpException();
93
        }
94
95
        return Rate::fromRateInterface($this->repository->get($request->get('currency_code'), \DateTime::createFromFormat('Y-m-d', $request->get('date')), $request->get('rate_type')));
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFor... $request->get('date')) targeting DateTime::createFromFormat() can also be of type false; however, RunOpenCode\ExchangeRate...ositoryInterface::get() does only seem to accept null|object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
96
    }
97
98
}
99