Completed
Push — master ( 2f124c...66b0c2 )
by Mohamed
11:36 queued 09:28
created

AbstractCommand::getRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Moo\FlashCardBundle package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moo\FlashCardBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * AbstractCommand contains abstracted/helper methods that are needed for a command line object.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 */
22
abstract class AbstractCommand extends ContainerAwareCommand
23
{
24
    /**
25
     * An instance of Validator
26
     *
27
     * @var \Symfony\Component\Validator\Validator
28
     */
29
    protected $validator;
30
31
    /**
32
     * Shortcut to return the Doctrine Registry service.
33
     *
34
     * @return \Doctrine\Bundle\DoctrineBundle\Registry
35
     */
36
    protected function getDoctrine()
37
    {
38
        return $this->getContainer()->get('doctrine');
39
    }
40
41
    /**
42
     * Shortcut to return an entity repository class.
43
     *
44
     * @return \Doctrine\ORM\EntityRepository
45
     */
46
    protected function getRepository($name)
47
    {
48
        return $this->getContainer()->get('moo_flashcard.' . $name . '.repository');
49
    }
50
51
    /**
52
     * Shortcut to the translator method
53
     *
54
     * @param string $message
55
     * @param array  $params
56
     */
57
    protected function trans($message, $params = [])
58
    {
59
        return $this->getContainer()->get('translator')->trans($message, $params);
60
    }
61
62
    /**
63
     * Writes an error message to the output and adds a newline at the end.
64
     *
65
     * @param OutputInterface $output
66
     * @param string          $message
67
     * @param array           $params
68
     *
69
     * @return mixed
70
     */
71
    protected function error(OutputInterface $output, $message, $params = [])
72
    {
73
        return $output->writeln('<error>' . $this->trans($message, $params) . '</error>');
74
    }
75
76
    /**
77
     * Writes an success message to the output and adds a newline at the end.
78
     *
79
     * @param OutputInterface $output
80
     * @param string          $message
81
     * @param array           $params
82
     *
83
     * @return mixed
84
     */
85
    protected function success(OutputInterface $output, $message, $params = [])
86
    {
87
        return $output->writeln('<info>' . $this->trans($message, $params) . '</info>');
88
    }
89
90
    /**
91
     * Get instance of Validator
92
     *
93
     * @return \Symfony\Component\Validator\Validator
94
     */
95
    protected function getValidator()
96
    {
97
        if (null === $this->validator) {
98
            $this->validator = $this->getContainer()->get('validator');
99
        }
100
101
        return $this->validator;
102
    }
103
104
    /**
105
     * Validate a property of an entity
106
     *
107
     * @param object $entity
108
     * @param string $propertyName
109
     * @param null   $groups
110
     *
111
     * @return bool
112
     */
113
    protected function validate($entity, $propertyName, $groups = null)
114
    {
115
        $violations = $this->getValidator()->validateProperty($entity, $propertyName, $groups);
116
        if (count($violations) > 0) {
117
            return $violations[0];
118
        }
119
120
        return true;
121
    }
122
}
123