Completed
Push — master ( 616f83...c8d577 )
by Gilles
13s
created

RewriteUrl   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A preActivation() 0 12 2
B update() 0 36 6
A getUnknownSources() 0 10 3
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the RewriteUrl module for Thelia.                       */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace RewriteUrl;
14
15
use Propel\Runtime\Connection\ConnectionInterface;
16
use Symfony\Component\Finder\Finder;
17
use Thelia\Model\ConfigQuery;
18
use Thelia\Model\Map\RewritingUrlTableMap;
19
use Thelia\Model\RewritingUrl;
20
use Thelia\Model\RewritingUrlQuery;
21
use Thelia\Module\BaseModule;
22
use Thelia\Install\Database;
23
24
/**
25
 * Class RewriteUrl
26
 * @package RewriteUrl
27
 * @author Vincent Lopes <[email protected]>
28
 * @author Gilles Bourgeat <[email protected]>
29
 */
30
class RewriteUrl extends BaseModule
31
{
32
    /** @var string  */
33
    const MODULE_DOMAIN = "rewriteurl";
34
35
    /** @var string  */
36
    const MODULE_NAME = "rewriteurl";
37
38
    /* @var string */
39
    const UPDATE_PATH = __DIR__ . DS . 'Config' . DS . 'update';
40
41
    /** @static null|array */
42
    static protected $unknownSources;
43
44
45
46
    public function preActivation(ConnectionInterface $con = null)
47
    {
48
        if (!$this->getConfigValue('is_initialized', false)) {
49
            $database = new Database($con);
50
51
            $database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
52
53
            $this->setConfigValue('is_initialized', true);
54
        }
55
56
        return true;
57
    }
58
59
    /**
60
     * @param string $currentVersion
61
     * @param string $newVersion
62
     * @param ConnectionInterface $con
63
     * @throws \Exception
64
     * @throws \Propel\Runtime\Exception\PropelException
65
     * @since 1.2.3
66
     */
67
    public function update($currentVersion, $newVersion, ConnectionInterface $con = null)
68
    {
69
        $finder = (new Finder())->files()->name('#.*?\.sql#')->sortByName()->in(self::UPDATE_PATH);
70
71
        if ($finder->count() === 0) {
72
            return;
73
        }
74
75
        $database = new Database($con);
76
77
        /** @var \Symfony\Component\Finder\SplFileInfo $updateSQLFile */
78
        foreach ($finder as $updateSQLFile) {
79
            if (version_compare($currentVersion, str_replace('.sql', '', $updateSQLFile->getFilename()), '<')) {
80
                $database->insertSql(null, [$updateSQLFile->getPathname()]);
81
            }
82
        }
83
84
        /*
85
         * Fix for urls that redirect on itself
86
         */
87
        $urls = RewritingUrlQuery::create()
88
            ->where(RewritingUrlTableMap::ID . " = " . RewritingUrlTableMap::REDIRECTED)
89
            ->find();
90
91
        /** @var RewritingUrl $url */
92
        foreach ($urls as $url) {
93
            $parent = RewritingUrlQuery::create()
94
                ->filterByView($url->getView())
95
                ->filterByViewId($url->getViewId())
96
                ->filterByViewLocale($url->getViewLocale())
97
                ->filterByRedirected(null)
98
                ->findOne();
99
100
            $url->setRedirected(($parent === null) ? null : $parent->getId())->save();
101
        }
102
    }
103
104
    /**
105
     * @return array|null
106
     */
107
    public static function getUnknownSources()
108
    {
109
        if (static::$unknownSources === null) {
110
            static::$unknownSources = [];
111
            if (null !== $config = ConfigQuery::read('obsolete_rewriten_url_view', null)) {
112
                static::$unknownSources[] = $config;
113
            }
114
        }
115
        return static::$unknownSources;
116
    }
117
}
118