Version6Adapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 60
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4
ccs 22
cts 22
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B attach() 0 22 4
B handleAttaching() 0 28 2
1
<?php
2
namespace Buttress\ConcreteClient\Adapter;
3
4
use Buttress\ConcreteClient\Connection\LegacyConnection;
5
use Buttress\ConcreteClient\Exception\RuntimeException;
6
use Buttress\ConcreteClient\Exception\VersionMismatchException;
7
use Buttress\ConcreteClient\Transaction\SilentTransaction;
8
use Buttress\ConcreteClient\Transaction\SimpleTransaction;
9
10
/**
11
 * An adapter that connects to legacy concrete5 sites
12
 * @todo Determine the lowest version of c5 we support
13
 */
14
class Version6Adapter implements Adapter
15
{
16
17
    /**
18
     * Attach to legacy concrete5
19
     * @throws \Buttress\ConcreteClient\Exception\VersionMismatchException
20
     * @throws \Buttress\ConcreteClient\Exception\RuntimeException
21
     */
22 8
    public function attach($path)
23
    {
24
        // If the PHP version is more than 5.6.999, we have the wrong version.
25 8
        if (version_compare(PHP_VERSION, '5.6.999', '>')) {
26 2
            throw VersionMismatchException::expected('PHP < 7.0.0', PHP_VERSION);
27
        }
28
29
        // Check if headers are sent
30 6
        if (headers_sent()) {
31 2
            throw new RuntimeException('Loading version 6 after headers are sent is not supported.');
32
        }
33
34
        // Check if we've installed
35 4
        if (!file_exists($path . '/config/site.php')) {
36 2
            throw new RuntimeException('Connecting to version 6 before installing is not supported.');
37
        }
38
39
        // Create a new silent transaction to handle connecting to concrete5
40 2
        return SilentTransaction::transact(function () use ($path) {
41 2
            return $this->handleAttaching($path);
42 2
        });
43
    }
44
45 2
    protected function handleAttaching($path)
1 ignored issue
show
Coding Style introduced by
handleAttaching uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46
    {
47
        // Change the cwd to the site path
48 2
        chdir($path);
49
50
        // Define a couple things concrete5 expects
51 2
        define('DIR_BASE', $path);
52 2
        define('C5_ENVIRONMENT_ONLY', true);
53
54
        // Set the error reporting low
55 2
        error_reporting(E_ALL | ~E_NOTICE | ~E_WARNING | ~E_STRICT);
56
57
        // Add 3rdparty to include path
58 2
        set_include_path(get_include_path() . PATH_SEPARATOR . $path . '/concrete/libraries/3rdparty');
59
60
        // Include Adodb first, not sure why this was needed
61 2
        require_once $path . '/concrete/libraries/3rdparty/adodb/adodb.inc.php';
62
63
        // Load in legacy dispatcher
64 2
        require_once $path . '/concrete/dispatcher.php';
65
66
        // Adodb Stuff
67 2
        $GLOBALS['ADODB_ASSOC_CASE'] = 2;
68 2
        $GLOBALS['ADODB_ACTIVE_CACHESECS'] = 300;
69 2
        $GLOBALS['ADODB_CACHE_DIR'] = defined('DIR_FILES_CACHE_DB') ? DIR_FILES_CACHE_DB : '';
70
71 2
        return new LegacyConnection();
72
    }
73
}
74