Skip to content

Commit a7f07e5

Browse files
authored
Merge pull request #7 from farzai/add-coverage
Refactor code
2 parents 687c24b + ec94bd0 commit a7f07e5

File tree

16 files changed

+172
-120
lines changed

16 files changed

+172
-120
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ psalm.xml
1313
vendor
1414
.php-cs-fixer.cache
1515
.env
16+
**/.DS_Store

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Viola PHP
22

3+
![Example CLI](assets/example.png)
4+
35
[![Latest Version on Packagist](https://img.shields.io/packagist/v/farzai/viola.svg?style=flat-square)](https://packagist.org/packages/farzai/viola)
46
[![Tests](https://img.shields.io/github/actions/workflow/status/farzai/viola-php/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/farzai/viola-php/actions/workflows/run-tests.yml)
57
[![Total Downloads](https://img.shields.io/packagist/dt/farzai/viola.svg?style=flat-square)](https://packagist.org/packages/farzai/viola)
68

79

810
Viola is a PHP package that allows you to ask questions to ChatGPT and get the answer with your own data.
911

10-
![Example CLI](assets/example.png)
11-
1212
## Requirements
1313

1414
- PHP >= 8.0
@@ -129,8 +129,8 @@ use Farzai\Viola\Viola;
129129
$viola = Viola::builder()
130130
->setApiKey(string $key)
131131
->setDatabaseConfig(string $driver, array $databaseConfig)
132-
->setClient(\Psr\Http\Client\ClientInterface $client)
133-
->setLogger(\Psr\Log\LoggerInterface $logger)
132+
->setClient(\Psr\Http\Client\ClientInterface $client) // Optional
133+
->setLogger(\Psr\Log\LoggerInterface $logger) // Optional
134134
->build();
135135
136136
// Ask a question to ChatGPT.

bin/viola

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ if (file_exists(__DIR__.'/../../../autoload.php')) {
77
require __DIR__.'/../vendor/autoload.php';
88
}
99

10+
11+
$storage = new Farzai\Viola\Storage\CacheFilesystemStorage();
12+
$databaseConfig = new Farzai\Viola\Storage\DatabaseConnectionRepository(
13+
new Farzai\Viola\Storage\CacheFilesystemStorage()
14+
);
15+
1016
$commands = [
11-
Farzai\Viola\Commands\Config::class,
12-
Farzai\Viola\Commands\ClearConfig::class,
13-
Farzai\Viola\Commands\ShowConfig::class,
14-
Farzai\Viola\Commands\UseConfig::class,
15-
Farzai\Viola\Commands\AskQuestion::class,
17+
new Farzai\Viola\Commands\Config('config', $storage, $databaseConfig),
18+
new Farzai\Viola\Commands\ClearConfig('config:clear', $storage, $databaseConfig),
19+
new Farzai\Viola\Commands\ShowConfig('config:show', $storage, $databaseConfig),
20+
new Farzai\Viola\Commands\UseConfig('use', $storage, $databaseConfig),
21+
new Farzai\Viola\Commands\AskQuestion('ask', $storage, $databaseConfig),
1622
];
1723

1824
$application = new Symfony\Component\Console\Application();
1925

2026
foreach ($commands as $command) {
21-
$application->add(new $command());
27+
$application->add($command);
2228
}
2329

2430
$application->run();

src/Builder.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function setDatabaseConfig(string $driver, array $config)
9191

9292
if (! in_array($driver, $availableDrivers)) {
9393
throw new \InvalidArgumentException(
94-
"Unsupported driver [{$driver}]"
94+
"Unsupported driver [{$driver}]."
9595
);
9696
}
9797

@@ -145,7 +145,7 @@ public function build(): Viola
145145
}
146146

147147
if (! $this->connector) {
148-
$this->connector = (new ConnectorFactory())->create($this->config['database']['driver']);
148+
$this->connector = $this->createConnector($this->config['database']['driver']);
149149
}
150150

151151
$connection = $this->connector->connect($this->config['database']['connection']);
@@ -157,4 +157,14 @@ public function build(): Viola
157157
$this->logger,
158158
);
159159
}
160+
161+
/**
162+
* Create a connector instance based on the configuration.
163+
*/
164+
private function createConnector(string $driver)
165+
{
166+
$connector = (new ConnectorFactory())->create($driver);
167+
168+
return $connector;
169+
}
160170
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Symfony\Component\Console\Question\ConfirmationQuestion;
1111
use Symfony\Component\Console\Question\Question;
1212

13-
abstract class Command extends SymfonyCommand
13+
abstract class AbstractCommand extends SymfonyCommand
1414
{
1515
protected InputInterface $input;
1616

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Farzai\Viola\Commands;
4+
5+
use Farzai\Viola\Contracts\DatabaseConnectionRepositoryInterface;
6+
use Farzai\Viola\Contracts\StorageRepositoryInterface;
7+
8+
abstract class AbstractContextCommand extends AbstractCommand
9+
{
10+
public function __construct(
11+
string $name,
12+
protected StorageRepositoryInterface $storage,
13+
protected DatabaseConnectionRepositoryInterface $databaseConfig
14+
) {
15+
parent::__construct($name);
16+
}
17+
}

src/Commands/AskQuestion.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,21 @@
22

33
namespace Farzai\Viola\Commands;
44

5-
use Farzai\Viola\Storage\CacheFilesystemStorage;
6-
use Farzai\Viola\Storage\DatabaseConnectionRepository;
75
use Farzai\Viola\Viola;
86
use Psr\Log\NullLogger;
97
use Symfony\Component\Console\Input\InputArgument;
108
use Symfony\Component\Console\Logger\ConsoleLogger;
119

12-
class AskQuestion extends Command
10+
class AskQuestion extends AbstractContextCommand
1311
{
14-
protected static $defaultName = 'ask';
15-
16-
/**
17-
* @var \Farzai\Viola\Contracts\StorageRepositoryInterface
18-
*/
19-
private $storage;
20-
21-
/**
22-
* @var \Farzai\Viola\Contracts\DatabaseConnectionRepositoryInterface
23-
*/
24-
private $databaseConfig;
25-
2612
protected function configure()
2713
{
2814
$this
2915
->setDescription('Ask a question to the ChatGPT')
3016
->addArgument('question', InputArgument::REQUIRED, 'The question to ask')
3117
->setHelp("This command allows you to ask a question to the ChatGPT.\nYou may run `config:show` to see available connections for confirmation.")
18+
->addOption('only-result', null, null, 'Do not display the answer')
3219
->addOption('debug', 'd', null, 'Display the debug information');
33-
34-
$this->storage = new CacheFilesystemStorage();
35-
$this->databaseConfig = new DatabaseConnectionRepository(new CacheFilesystemStorage());
3620
}
3721

3822
protected function handle(): int
@@ -53,13 +37,17 @@ protected function handle(): int
5337
)
5438
->build();
5539

56-
$response = $viola->ask($question);
40+
$response = $viola
41+
->onlyResults((bool) $this->input->getOption('only-result'))
42+
->ask($question);
5743

5844
$this->info($response->getAnswer());
5945

60-
$this->displayAsTable($response->getResults());
46+
if (count($response->getResults())) {
47+
$this->displayAsTable($response->getResults());
48+
}
6149

62-
return Command::SUCCESS;
50+
return static::SUCCESS;
6351
}
6452

6553
private function ensureDatabaseIsConfigured()

src/Commands/ClearConfig.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,10 @@
22

33
namespace Farzai\Viola\Commands;
44

5-
use Farzai\Viola\Contracts\StorageRepositoryInterface;
6-
use Farzai\Viola\Storage\CacheFilesystemStorage;
75
use Symfony\Component\Console\Input\InputArgument;
86

9-
class ClearConfig extends Command
7+
class ClearConfig extends AbstractContextCommand
108
{
11-
protected static $defaultName = 'config:clear';
12-
13-
private StorageRepositoryInterface $storage;
14-
15-
/**
16-
* Create a new instance.
17-
*/
18-
public function __construct()
19-
{
20-
parent::__construct();
21-
22-
$this->storage = new CacheFilesystemStorage();
23-
}
24-
259
protected function configure()
2610
{
2711
$this
@@ -48,28 +32,28 @@ protected function handle(): int
4832

4933
$this->info('All connections cleared.');
5034

51-
return Command::SUCCESS;
35+
return static::SUCCESS;
5236
}
5337

5438
$this->info('Cancelled.');
5539

56-
return Command::SUCCESS;
40+
return static::SUCCESS;
5741
}
5842

5943
if (! $this->storage->has("database.connections.{$connection}")) {
6044
$this->error("Connection {$connection} does not exist.");
6145

62-
return Command::FAILURE;
46+
return static::FAILURE;
6347
}
6448

6549
if ($this->confirm("Are you sure you want to clear connection {$connection}?", false)) {
6650
$this->storage->remove("database.connections.{$connection}");
6751

6852
$this->info("Connection {$connection} cleared.");
6953

70-
return Command::SUCCESS;
54+
return static::SUCCESS;
7155
}
7256

73-
return Command::SUCCESS;
57+
return static::SUCCESS;
7458
}
7559
}

src/Commands/Config.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,13 @@
22

33
namespace Farzai\Viola\Commands;
44

5-
use Farzai\Viola\Contracts\DatabaseConnectionRepositoryInterface;
6-
use Farzai\Viola\Contracts\StorageRepositoryInterface;
75
use Farzai\Viola\Database\ConnectorFactory;
8-
use Farzai\Viola\Storage\CacheFilesystemStorage;
9-
use Farzai\Viola\Storage\DatabaseConnectionRepository;
106

11-
class Config extends Command
7+
class Config extends AbstractContextCommand
128
{
13-
protected static $defaultName = 'config';
14-
15-
private StorageRepositoryInterface $storage;
16-
17-
private DatabaseConnectionRepositoryInterface $databaseConfig;
18-
199
protected function configure()
2010
{
2111
$this->setDescription('Config API key and database connection.');
22-
23-
$this->storage = new CacheFilesystemStorage();
24-
$this->databaseConfig = new DatabaseConnectionRepository(new CacheFilesystemStorage());
2512
}
2613

2714
protected function handle(): int
@@ -40,7 +27,7 @@ protected function handle(): int
4027
} else {
4128
$this->error('API key is required.');
4229

43-
return Command::FAILURE;
30+
return static::FAILURE;
4431
}
4532

4633
$this->info('Setting up database connection...');
@@ -50,7 +37,7 @@ protected function handle(): int
5037
if (! $connectionName || empty($connectionName)) {
5138
$this->error('Database connection name is required.');
5239

53-
return Command::FAILURE;
40+
return static::FAILURE;
5441
}
5542

5643
$connections = $this->databaseConfig->all();
@@ -62,7 +49,7 @@ protected function handle(): int
6249
if (! $this->confirm("Connection {$connectionName} already exists. Do you want to continue edit it?")) {
6350
$this->info('Cancelled.');
6451

65-
return Command::SUCCESS;
52+
return static::SUCCESS;
6653
}
6754
}
6855
}
@@ -82,7 +69,7 @@ protected function handle(): int
8269
if (! isset($stubConfig['drivers'][$driver])) {
8370
$this->error('Only support '.implode(', ', array_keys($stubConfig['drivers'])).' driver.');
8471

85-
return Command::FAILURE;
72+
return static::FAILURE;
8673
}
8774

8875
$stubConfig = $stubConfig['drivers'][$driver];
@@ -124,7 +111,7 @@ protected function handle(): int
124111

125112
$this->info('Connection successful!, Platform: '.$platform);
126113

127-
return Command::SUCCESS;
114+
return static::SUCCESS;
128115
} catch (\Exception $e) {
129116
$this->error('Connection failed!, '.$e->getMessage());
130117
}
@@ -140,6 +127,6 @@ protected function handle(): int
140127
}
141128
}
142129

143-
return Command::SUCCESS;
130+
return static::SUCCESS;
144131
}
145132
}

src/Commands/ShowConfig.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,11 @@
22

33
namespace Farzai\Viola\Commands;
44

5-
use Farzai\Viola\Contracts\DatabaseConnectionRepositoryInterface;
6-
use Farzai\Viola\Contracts\StorageRepositoryInterface;
7-
use Farzai\Viola\Storage\CacheFilesystemStorage;
8-
use Farzai\Viola\Storage\DatabaseConnectionRepository;
9-
10-
class ShowConfig extends Command
5+
class ShowConfig extends AbstractContextCommand
116
{
12-
protected static $defaultName = 'config:show';
13-
14-
private StorageRepositoryInterface $storage;
15-
16-
private DatabaseConnectionRepositoryInterface $databaseConfig;
17-
187
protected function configure()
198
{
20-
$this
21-
->setDescription('Show current database connection and API key.');
22-
23-
$this->storage = new CacheFilesystemStorage();
24-
$this->databaseConfig = new DatabaseConnectionRepository(new CacheFilesystemStorage());
9+
$this->setDescription('Show current database connection and API key.');
2510
}
2611

2712
protected function handle(): int
@@ -30,7 +15,7 @@ protected function handle(): int
3015
if (! $apiKey) {
3116
$this->error('API key is not set.');
3217

33-
return Command::FAILURE;
18+
return static::FAILURE;
3419
}
3520

3621
$this->info("API Key: <comment>{$apiKey}</comment>");
@@ -49,6 +34,6 @@ protected function handle(): int
4934
$this->info('Database Connection: <comment>Not set</comment>');
5035
}
5136

52-
return Command::SUCCESS;
37+
return static::SUCCESS;
5338
}
5439
}

0 commit comments

Comments
 (0)