Completed
Push — master ( 46acee...8a5224 )
by Thomas
02:09
created

Container.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Shopware\Components\Api\Manager;
4
use TEiling\Scd16\Resource\ArticleResourceInterface;
5
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
6
use TEiling\Scd16\Cache\CacheInterface;
7
use TEiling\Scd16\Cache\ShopwareCache;
8
9
class Shopware_Plugins_Core_Scd16_Container
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
12
    /** @var ArticleResourceInterface */
13
    private $articleResource;
14
15
    /** @var Zend_Db_Adapter_Abstract */
16
    private $db;
17
18
    /**
19
     * @var \Symfony\Component\DependencyInjection\ContainerInterface $container
20
     */
21
    private $container;
22
23
    /**
24
     * @var Zend_Cache_Core $cache
25
     */
26
    private $cache;
27
28
    /**
29
     * Shopware_Plugins_Core_Scd16_Container constructor.
30
     *
31
     * @param ArticleResourceInterface $articleResource
32
     * @param Zend_Db_Adapter_Abstract $db
33
     * @param CacheInterface $cache
34
     *
35
     * @throws \Exception
36
     * @throws \Zend_Cache_Exception
37
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
38
     */
39
    public function __construct(
40
        ArticleResourceInterface $articleResource = null,
41
        Zend_Db_Adapter_Abstract $db = null,
42
        CacheInterface $cache = null
43
    ) {
44
        $this->articleResource = $articleResource ?? Manager::getResource('Article');
45
        $this->db = $db ?? Shopware()->Db();
46
        $this->cache = $cache ?? new ShopwareCache(Shopware()->Cache());
0 ignored issues
show
Documentation Bug introduced by
It seems like $cache ?? new \TEiling\S...he(Shopware()->Cache()) of type object<TEiling\Scd16\Cache\CacheInterface> is incompatible with the declared type object<Zend_Cache_Core> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
48
        $cacheId = 'Scd16_Core_Container';
49
50
        if ($this->cache->test($cacheId)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->cache->test($cacheId) of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
51
            eval('?>' . $this->cache->load($cacheId));
52
            $container = new Scd16ServiceContainer();
53
        } else {
54
            $container = $this->createContainer();
55
            $container->compile();
56
57
            $dumper = new PhpDumper($container);
58
            $dumpParams = [
59
                'class' => 'Scd16ServiceContainer',
60
            ];
61
62
            $this->cache->save($dumper->dump($dumpParams), $cacheId, ['Shopware_Plugin'], 3600);
63
        }
64
65
        $container->set('article_res', $this->articleResource);
66
        $container->set('db', $this->db);
67
68
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container of type object<Scd16ServiceContainer> or object<Symfony\Component...ction\ContainerBuilder> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
    }
70
71
    /**
72
     * creates a Container
73
     *
74
     * @param string $dir
75
     *
76
     * @return \Symfony\Component\DependencyInjection\ContainerBuilder
77
     * @throws Exception
78
     */
79
    public function createContainer($dir = __DIR__)
80
    {
81
        $instance = new \Symfony\Component\DependencyInjection\ContainerBuilder();
82
83
        $loader = new \Symfony\Component\DependencyInjection\Loader\XmlFileLoader(
84
            $instance,
85
            new \Symfony\Component\Config\FileLocator(realpath($dir))
86
        );
87
        $loader->load('container.xml');
88
89
        $instance->setParameter(
90
            'config.path.image',
91
            Shopware()->Plugins()->Core()->Scd16()->Path() . '_vImages/'
92
        );
93
94
        $instance->setParameter(
95
            'config.path.csv',
96
            Shopware()->Plugins()->Core()->Scd16()->Path() . 'tests/fixtures/shopexport.csv'
97
        );
98
99
        return $instance;
100
    }
101
102
    /**
103
     * @param $id
104
     *
105
     * @return mixed
106
     */
107
    public function get($id)
108
    {
109
        return $this->container->get($id);
110
    }
111
}
112