LdapToolsCacheWarmer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 60
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A warmUp() 0 14 3
A isOptional() 0 4 1
A cacheAllLdapSchemaObjects() 0 7 2
1
<?php
2
/**
3
 * This file is part of the LdapToolsBundle package.
4
 *
5
 * (c) Chad Sikorra <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LdapTools\Bundle\LdapToolsBundle\CacheWarmer;
12
13
use LdapTools\Configuration;
14
use LdapTools\Factory\LdapObjectSchemaFactory;
15
use LdapTools\LdapManager;
16
use LdapTools\Schema\LdapObjectSchema;
17
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
18
19
/**
20
 * A cache warmer for the LDAP schema types.
21
 *
22
 * @author Chad Sikorra <[email protected]>
23
 */
24
class LdapToolsCacheWarmer implements CacheWarmerInterface
25
{
26
    /**
27
     * @var LdapManager
28
     */
29
    protected $ldap;
30
31
    /**
32
     * @var Configuration
33
     */
34
    protected $config;
35
36
    /**
37
     * @param LdapManager $ldap
38
     * @param Configuration $config
39
     */
40
    public function __construct(LdapManager $ldap, Configuration $config)
41
    {
42
        $this->ldap = $ldap;
43
        $this->config = $config;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function warmUp($cacheDir)
50
    {
51
        $domain = $this->ldap->getDomainContext();
52
        foreach ($this->config->getDomainConfiguration() as $domainConfig) {
0 ignored issues
show
Bug introduced by
The expression $this->config->getDomainConfiguration() of type object<LdapTools\DomainC...s\DomainConfiguration>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
53
            $this->ldap->switchDomain($domainConfig->getDomainName());
54
            $schemaFactory = $this->ldap->getSchemaFactory();
55
            $parser = $this->ldap->getSchemaParser();
56
            $schema = empty($domainConfig->getSchemaName()) ? $domainConfig->getLdapType() : $domainConfig->getSchemaName();
57
            $ldapObjects = $parser->parseAll($schema);
58
59
            $this->cacheAllLdapSchemaObjects($schemaFactory, ...$ldapObjects);
60
        }
61
        $this->ldap->switchDomain($domain);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function isOptional()
68
    {
69
        return true;
70
    }
71
72
    /**
73
     * @param LdapObjectSchemaFactory $schemaFactory
74
     * @param LdapObjectSchema ...$schemaObjects
75
     */
76
    protected function cacheAllLdapSchemaObjects(LdapObjectSchemaFactory $schemaFactory, LdapObjectSchema ...$schemaObjects)
77
    {
78
        /** @var LdapObjectSchema $ldapSchemaObject */
79
        foreach ($schemaObjects as $ldapSchemaObject) {
80
            $schemaFactory->get($ldapSchemaObject->getSchemaName(), $ldapSchemaObject->getObjectType());
81
        }
82
    }
83
}
84