Skip to content

Commit 4ff8636

Browse files
committed
Cleaned up
1 parent fbba31e commit 4ff8636

8 files changed

Lines changed: 157 additions & 30 deletions

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
Note: The "SFTP private key" key must be passwordless.
1313

14+
You can use `ssh-keygen` to remove the password from a certificate:
15+
16+
``` shell
17+
cp cert/sf2900-sftp cert/sf2900-sftp-nopass
18+
ssh-keygen -p -N "" -f cert/sf2900-sftp-nopass
19+
```
20+
1421
2. Go to `/admin/os2forms_fordelingskomponent/settings` and configure the Fordelingskomponent module.
1522

1623
## Console commands

drush.services.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,22 @@ services:
66
tags:
77
- { name: console.command }
88

9+
os2forms_fordelingskomponent.sftp.put:
10+
class: Drupal\os2forms_fordelingskomponent\Drush\Commands\SftpPutCommand
11+
arguments:
12+
- '@Drupal\os2forms_fordelingskomponent\Helper\FordelingskomponentHelper'
13+
tags:
14+
- { name: console.command }
15+
16+
os2forms_fordelingskomponent.sftp.get:
17+
class: Drupal\os2forms_fordelingskomponent\Drush\Commands\SftpGetCommand
18+
arguments:
19+
- '@Drupal\os2forms_fordelingskomponent\Helper\FordelingskomponentHelper'
20+
tags:
21+
- { name: console.command }
22+
923
os2forms_fordelingskomponent.sned.journalnotat:
10-
class: Drupal\os2forms_fordelingskomponent\Drush\Commands\SendJournalnotat
24+
class: Drupal\os2forms_fordelingskomponent\Drush\Commands\SendJournalnotatCommand
1125
arguments:
1226
- '@Drupal\os2forms_fordelingskomponent\Helper\FordelingskomponentHelper'
1327
tags:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\os2forms_fordelingskomponent\Drush\Commands;
6+
7+
use Drupal\os2forms_fordelingskomponent\Helper\FordelingskomponentHelper;
8+
use Symfony\Component\Console\Command\Command;
9+
10+
/**
11+
* Abstract base command.
12+
*/
13+
abstract class AbstractCommand extends Command {
14+
15+
public function __construct(
16+
protected readonly FordelingskomponentHelper $helper,
17+
) {
18+
parent::__construct();
19+
}
20+
21+
}

src/Drush/Commands/SendJournalnotatCommand.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44

55
namespace Drupal\os2forms_fordelingskomponent\Drush\Commands;
66

7-
use SF2900\SftpHelper;
87
use Drupal\os2forms_fordelingskomponent\Helper\FordelingskomponentHelper;
98
use Symfony\Component\Console\Attribute\AsCommand;
109
use Symfony\Component\Console\Command\Command;
11-
use Symfony\Component\Console\Input\InputArgument;
1210
use Symfony\Component\Console\Input\InputInterface;
1311
use Symfony\Component\Console\Output\OutputInterface;
14-
use Symfony\Component\Console\Style\SymfonyStyle;
1512

1613
// phpcs:disable Drupal.Commenting.ClassComment.Missing
1714
#[AsCommand(
@@ -32,27 +29,12 @@ public function __construct(
3229
* @see https://www.drush.org/13.x/commands/
3330
*/
3431
protected function configure(): void {
35-
$this
36-
->addArgument('dir', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'List of directory paths', [SftpHelper::INCOMING_FOLDER]);
3732
}
3833

3934
/**
4035
* {@inheritdoc}
4136
*/
4237
protected function execute(InputInterface $input, OutputInterface $output): int {
43-
$io = new SymfonyStyle($input, $output);
44-
$sftp = $this->helper->sf2900()->sftp();
45-
$dirs = (array) $input->getArgument('dir');
46-
foreach ($dirs as $dir) {
47-
// @todo getFiles does not complain when using an invalid directory …
48-
$files = $sftp->getFiles($dir);
49-
$files = array_filter($files, fn (string $file) => !preg_match('/^[.]+$/', $file));
50-
$io->section($dir);
51-
foreach ($files as $file) {
52-
$io->writeln($file);
53-
}
54-
}
55-
5638
return self::SUCCESS;
5739
}
5840

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\os2forms_fordelingskomponent\Drush\Commands;
6+
7+
use ItkDev\Serviceplatformen\Service\SF2900\SF2900\SftpHelper;
8+
use Symfony\Component\Console\Attribute\AsCommand;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Style\SymfonyStyle;
13+
14+
// phpcs:disable Drupal.Commenting.ClassComment.Missing
15+
#[AsCommand(
16+
name: 'os2forms-fordelingskomponent:sftp:get',
17+
description: 'Get file from SFTP server',
18+
)]
19+
final class SftpGetCommand extends AbstractCommand {
20+
21+
/**
22+
* {@inheritdoc}
23+
*
24+
* @see https://www.drush.org/13.x/commands/
25+
*/
26+
protected function configure(): void {
27+
$this
28+
->addArgument('filename', InputArgument::REQUIRED, 'Name of file to get')
29+
->addArgument('dir', InputArgument::OPTIONAL, 'Directory', SftpHelper::OUTGOING_FOLDER);
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
protected function execute(InputInterface $input, OutputInterface $output): int {
36+
$io = new SymfonyStyle($input, $output);
37+
$sftp = $this->helper->sf2900()->sftp();
38+
39+
$filename = $input->getArgument('filename');
40+
$dir = $input->getArgument('dir');
41+
42+
try {
43+
$contents = $sftp->getContents($filename, $dir);
44+
45+
echo $contents;
46+
47+
return self::SUCCESS;
48+
}
49+
catch (\Exception $exception) {
50+
$io->error($exception->getMessage());
51+
52+
return self::FAILURE;
53+
}
54+
}
55+
56+
}

src/Drush/Commands/SftpLsCommand.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace Drupal\os2forms_fordelingskomponent\Drush\Commands;
66

7-
use SF2900\SftpHelper;
8-
use Drupal\os2forms_fordelingskomponent\Helper\FordelingskomponentHelper;
7+
use ItkDev\Serviceplatformen\Service\SF2900\SF2900\SftpHelper;
98
use Symfony\Component\Console\Attribute\AsCommand;
10-
use Symfony\Component\Console\Command\Command;
119
use Symfony\Component\Console\Input\InputArgument;
1210
use Symfony\Component\Console\Input\InputInterface;
1311
use Symfony\Component\Console\Output\OutputInterface;
@@ -18,13 +16,7 @@
1816
name: 'os2forms-fordelingskomponent:sftp:ls',
1917
description: 'List files on SFTP server',
2018
)]
21-
final class SftpLsCommand extends Command {
22-
23-
public function __construct(
24-
private readonly FordelingskomponentHelper $helper,
25-
) {
26-
parent::__construct();
27-
}
19+
final class SftpLsCommand extends AbstractCommand {
2820

2921
/**
3022
* {@inheritdoc}
@@ -33,7 +25,7 @@ public function __construct(
3325
*/
3426
protected function configure(): void {
3527
$this
36-
->addArgument('dir', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'List of directory paths', [SftpHelper::INCOMING_FOLDER]);
28+
->addArgument('dir', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'List of directory paths', [SftpHelper::OUTGOING_FOLDER]);
3729
}
3830

3931
/**
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\os2forms_fordelingskomponent\Drush\Commands;
6+
7+
use ItkDev\Serviceplatformen\Service\SF2900\SF2900\SftpHelper;
8+
use Symfony\Component\Console\Attribute\AsCommand;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Style\SymfonyStyle;
13+
14+
// phpcs:disable Drupal.Commenting.ClassComment.Missing
15+
#[AsCommand(
16+
name: 'os2forms-fordelingskomponent:sftp:put',
17+
description: 'Put file on SFTP server',
18+
)]
19+
final class SftpPutCommand extends AbstractCommand {
20+
21+
/**
22+
* {@inheritdoc}
23+
*
24+
* @see https://www.drush.org/13.x/commands/
25+
*/
26+
protected function configure(): void {
27+
$this
28+
->addArgument('filename', InputArgument::REQUIRED, 'Name of file to put')
29+
->addArgument('dir', InputArgument::OPTIONAL, 'Target directory', [SftpHelper::OUTGOING_FOLDER]);
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
protected function execute(InputInterface $input, OutputInterface $output): int {
36+
$io = new SymfonyStyle($input, $output);
37+
$sftp = $this->helper->sf2900()->sftp();
38+
39+
$filename = $input->getArgument('filename');
40+
41+
try {
42+
$result = $sftp->putFile($filename);
43+
$io->success(sprintf('File %s put on SFTP server as %s', $filename, $result));
44+
45+
return self::SUCCESS;
46+
}
47+
catch (\Exception $exception) {
48+
$io->error($exception->getMessage());
49+
50+
return self::FAILURE;
51+
}
52+
}
53+
54+
}

src/Helper/FordelingskomponentHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ public function sf2900(): SF2900 {
272272
$privateKeyKey = $this->keyRepository->getKey($options['sftp']['private_key']);
273273
$privateKey = $privateKeyKey->getKeyValue();
274274
$sf2900options = [
275+
'test_mode' => (bool) $this->getModuleConfig()->get('test_mode'),
275276
'authority_cvr' => $options['sender_id'],
276277
'certificate' => $certificate,
277278
'sftp' => [

0 commit comments

Comments
 (0)