TestRepository::findTests()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 18
cts 18
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 7
nop 1
crap 7
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 10
    public function findTests($search = null)
14
    {
15 10
        if ($search === null || empty($search)) {
16 6
            return $this->findAll();
17
        }
18
        
19 4
        if (!is_array($search)) {
20 1
            $search = [$search];
21 1
        }
22
        
23 4
        $tests = [];
24
        
25 4
        foreach ($search as $term) {
26
            //Find test by name first
27 4
            $test = $this->findByName($term);
28 4
            if ($test !== null) {
29 3
                $tests[] = $test;
30 3
                continue;
31
            }
32
             
33 3
            $testsInGroup = $this->findByGroupName($term);
34 3
            foreach ($testsInGroup as $test) {
35 2
                $tests[] = $test;
36 3
            }
37 4
        }
38
        
39 4
        return $tests;
40
    }
41
    
42 5
    public function findByName($name)
43
    {
44 5
        return $this->findOneBy([
45
            'name' => $name
46 5
        ]);
47
    }
48
    
49 5
    public function findByGroupName($name)
50
    {
51 5
        $group = $this->_em->getRepository('OverwatchTestBundle:TestGroup')
52 5
            ->findOneBy([
53
                'name' => $name
54 5
            ]);
55
56 5
        if ($group === null) {
57 2
            return [];
58
        }
59
        
60 3
        return $this->findBy([
61
            'group' => $group
62 3
        ]);
63
    }
64
}
65