Connection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 46.67%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 103
ccs 14
cts 30
cp 0.4667
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isConnected() 0 4 1
A getGateway() 0 4 1
A setGateway() 0 4 1
A getUsername() 0 4 1
A getName() 0 4 1
A __construct() 0 10 1
A run() 0 13 4
A status() 0 4 2
1
<?php
2
3
/*
4
 * This file is part of Rocketeer
5
 *
6
 * (c) Maxime Fabre <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace Rocketeer\Services\Connections\Connections;
14
15
use Closure;
16
use League\Flysystem\AdapterInterface;
17
use phpseclib\Net\SFTP;
18
use Rocketeer\Services\Connections\Credentials\Keys\ConnectionKey;
19
use Rocketeer\Services\Connections\TimeOutException;
20
use Rocketeer\Services\Filesystem\ConnectionKeyAdapterFactory;
21
22
/**
23
 * Represents a connection to a server and stage.
24
 */
25
class Connection extends AbstractConnection
26
{
27
    /**
28
     * @var AdapterInterface
29
     */
30
    protected $adapter;
31
32
    /**
33
     * Create a new SSH connection instance.
34
     *
35
     * @param ConnectionKey $connectionKey
36
     */
37 6
    public function __construct(ConnectionKey $connectionKey)
38 6
    {
39 6
        $this->roles = (array) $connectionKey->roles;
40 6
        $this->setConnectionKey($connectionKey);
41
42 6
        $adapter = new ConnectionKeyAdapterFactory();
43 6
        $adapter = $adapter->getAdapter($connectionKey);
44
45 6
        parent::__construct($adapter);
46 6
    }
47
48
    ////////////////////////////////////////////////////////////////////////////////
49
    ///////////////////////////// GETTERS AND SETTERS //////////////////////////////
50
    ////////////////////////////////////////////////////////////////////////////////
51
52
    /**
53
     * @return bool
54
     */
55 1
    public function isConnected()
56
    {
57 1
        return $this->adapter->isConnected();
0 ignored issues
show
Bug introduced by Maxime Fabre
It seems like you code against a concrete implementation and not the interface League\Flysystem\AdapterInterface as the method isConnected() does only exist in the following implementations of said interface: League\Flysystem\Adapter\AbstractFtpAdapter, League\Flysystem\Adapter\Ftp, League\Flysystem\Adapter\Ftpd, League\Flysystem\Adapter\SynologyFtp, League\Flysystem\Sftp\SftpAdapter.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
58
    }
59
60
    /**
61
     * Get the SFTP connection.
62
     *
63
     * @return SFTP
64
     */
65
    public function getGateway()
66
    {
67
        return $this->adapter->getConnection();
0 ignored issues
show
Bug introduced by Maxime Fabre
It seems like you code against a concrete implementation and not the interface League\Flysystem\AdapterInterface as the method getConnection() does only exist in the following implementations of said interface: League\Flysystem\Adapter\AbstractFtpAdapter, League\Flysystem\Adapter\Ftp, League\Flysystem\Adapter\Ftpd, League\Flysystem\Adapter\SynologyFtp, League\Flysystem\Sftp\SftpAdapter.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
68
    }
69
70
    /**
71
     * @param SFTP $gateway
72
     */
73
    public function setGateway($gateway)
74
    {
75
        $this->adapter->setNetSftpConnection($gateway);
0 ignored issues
show
Bug introduced by Maxime Fabre
It seems like you code against a concrete implementation and not the interface League\Flysystem\AdapterInterface as the method setNetSftpConnection() does only exist in the following implementations of said interface: League\Flysystem\Sftp\SftpAdapter.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
76
    }
77
78
    /**
79
     * @return string
80
     */
81 2
    public function getName()
82
    {
83 2
        return $this->connectionKey->name;
84
    }
85
86
    /**
87
     * @return string|null
88
     */
89 2
    public function getUsername()
90
    {
91 2
        return $this->connectionKey->username;
92
    }
93
94
    ////////////////////////////////////////////////////////////////////////////////
95
    ////////////////////////////////// EXECUTION ///////////////////////////////////
96
    ////////////////////////////////////////////////////////////////////////////////
97
98
    /**
99
     * Run a set of commands against the connection.
100
     *
101
     * @param string|array $commands
102
     * @param Closure|null $callback
103
     */
104
    public function run($commands, Closure $callback = null)
105
    {
106
        $gateway = $this->getGateway();
107
        $commands = is_array($commands) ? implode(' && ', $commands) : $commands;
108
        if ($this->connectionKey->isFtp()) {
109
            return;
110
        }
111
112
        $gateway->exec($commands, $callback);
113
        if ($gateway->isTimeout()) {
114
            throw new TimeOutException($gateway->timeout);
115
        }
116
    }
117
118
    /**
119
     * Get the exit status of the last command.
120
     *
121
     * @return int|bool
122
     */
123
    public function status()
124
    {
125
        return $this->connectionKey->isFtp() ? 0 : $this->getGateway()->getExitStatus();
126
    }
127
}
128