Skip to content

A little about architecture #5

@vasilevrv

Description

@vasilevrv

Hello!

  1. I would recommend using the factory pattern to create a custom client.

For example the final code might look like this:

 $sftpClientFactory = $this->get('nw.sftp');
 $sftp = $sftpClientFactory->create($host, $port);
 $sftp->login($username, $password);
 $sftp->loginWithKey($host, $username, $pubkeyfile, $privkeyfile, $passphrase = null);

What problems will it solve?

  • You can use more than one connection at the same time. When designing, it should always be considered that if one connection is required at the moment, then most likely in the future more will need to be used.
  • The service cannot store state by its own architecture. And if it stores the state (if absolutely necessary) then you need to implement the ResetInterface interface from the Symfony package. Otherwise it can cause problems when using an immortal lifecycle (RoadRunner, ReactPHP, etc).
  1. You need rewrite disconnect function. Please use https://www.php.net/manual/en/function.ssh2-disconnect.php, otherwise it can create big problems when using an long lifecycle.

  2. You cannot use file_get_contents() / file_put_contents() for large files. when used, the file is read entirely into memory and when working with large files, memory will run out. use fread() / fwrite() instead.

  3. Need to handle authentication errors according https://www.php.net/manual/en/function.ssh2-auth-password.php . For example, you can use custom error handler for getting error message.

  4. I didn't quite understand how the commands work, given that the connection is not being established.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions