Completed
Push — master ( 61044a...94728c )
by Rafał
24:26 queued 10:49
created

AbstractFixture::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Fixtures Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\FixturesBundle;
16
17
use Doctrine\Bundle\FixturesBundle\Fixture;
18
use SWP\Component\MultiTenancy\Exception\TenantNotFoundException;
19
use SWP\Component\MultiTenancy\Model\TenantInterface;
20
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
21
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
22
23
/**
24
 * Abstract fixture class.
25
 */
26
abstract class AbstractFixture extends Fixture implements ContainerAwareInterface
27
{
28
    const DEFAULT_TENANT_DOMAIN = 'localhost';
29
30
    use ContainerAwareTrait;
31
32
    /**
33
     * Get current kernel environment.
34
     *
35
     * @return string Environment type
36
     */
37
    public function getEnvironment()
38 104
    {
39
        return $this->container->getParameter('fixtures_type');
40 104
    }
41 104
42
    /**
43
     * @param $paths
44
     *
45
     * @return mixed
46
     */
47
    public function loadFixtures($paths)
48 104
    {
49
        $loader = $this->container->get('fidry_alice_data_fixtures.loader.doctrine');
50 104
51
        return $loader->load($this->locateResources($paths));
52
    }
53
54
    /**
55
     * Finds the PHPCR node by given id/path.
56
     *
57
     * @param string|null $className Document class name
58
     * @param string      $id        PHPCR path
59
     *
60 104
     * @return string|null
61
     */
62 104
    public function find($className, $id)
63
    {
64
        return $this->container->get('document_manager')->find($className, $id);
65
    }
66
67
    /**
68
     * Finds the PHPCR node by given id/path.
69
     *
70
     * @param string|null $className Document class name
71
     * @param string      $id        PHPCR path
72
     *
73
     * @return string|null
74
     */
75
    public function findByTenant($className, $id)
76
    {
77
        return $this->find($className, $this->generatePath($id));
78
    }
79
80
    /**
81
     * Generates tenant aware path.
82
     *
83
     * @param string $id
84
     *
85
     * @return string
86
     */
87
    public function generatePath($id)
88
    {
89
        return $this->getTenantPrefix(self::DEFAULT_TENANT_DOMAIN).'/'.ltrim($id, '/');
90
    }
91
92
    public function getRouteByName($id)
93
    {
94
        return $this->container->get('swp.provider.route')->getRouteByName($id);
95
    }
96
97
    /**
98
     * Gets current tenant's prefix.
99
     *
100
     * @param string $subdomain
0 ignored issues
show
Documentation introduced by
There is no parameter named $subdomain. Did you maybe mean $domain?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
101
     *
102
     * @return string
103 8
     */
104
    public function getTenantPrefix($domain)
105 8
    {
106
        /** @var TenantInterface $tenant */
107
        $tenant = $this->container->get('swp.repository.tenant')->findOneByDomain($domain);
108
109
        if (null === $tenant) {
110
            throw new TenantNotFoundException($domain);
111
        }
112
113
        return $tenant->getId();
114
    }
115
116
    /**
117
     * Locates the fixtures resources.
118
     *
119
     * @param array|string $paths Fixtures path(s)
120
     *
121
     * @return array|string the path(s)
122
     */
123
    protected function locateResources($paths)
124
    {
125
        $kernel = $this->container->get('kernel');
126
        if (is_array($paths)) {
127
            foreach ($paths as $key => $path) {
128
                $paths[$key] = $kernel->locateResource($path);
129
            }
130
        } else {
131
            $paths = $kernel->locateResource($paths);
132
        }
133
134 104
        return $paths;
135
    }
136
}
137