Failed Conditions
Pull Request — master (#142)
by Zac
04:15
created

TestRepository::findByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Overwatch\TestBundle\Entity;
4
5
use Doctrine\ORM\EntityRepository;
6
7
/**
8
 * TestRepository
9
 * Some shortcut and convienience methods to find tests
10
 */
11
class TestRepository extends EntityRepository
12
{
13
    public function findTests($search = null)
14
    {
15
        if ($search === null || empty($search)) {
16
            return $this->findAll();
17
        }
18
        
19
        if (!is_array($search)) {
20
            $search = [$search];
21
        }
22
        
23
        $tests = [];
24
        
25
        foreach ($search as $term) {
26
            //Find test by name first
27
            $test = $this->findByName($term);
28
            if ($test !== null) {
29
                $tests[] = $test;
30
                continue;
31
            }
32
             
33
            $testsInGroup = $this->findByGroupName($term);
34
            foreach ($testsInGroup as $test) {
35
                $tests[] = $test;
36
            }
37
        }
38
        
39
        return $tests;
40
    }
41
    
42
    public function findByName($name)
43
    {
44
        return $this->findOneBy([
45
            'name' => $name
46
        ]);
47
    }
48
    
49
    public function findByGroupName($name)
50
    {
51
        $group = $this->_em->getRepository('OverwatchTestBundle:TestGroup')
52
            ->findOneBy([
53
                'name' => $name
54
            ]);
55
56
        if ($group === null) {
57
            return [];
58
        }
59
        
60
        return $this->findBy([
61
            'group' => $group
62
        ]);
63
    }
64
}
65