Skip to content

Commit

Permalink
symfony output
Browse files Browse the repository at this point in the history
  • Loading branch information
blacksenator committed Apr 14, 2020
1 parent 2537ba7 commit bf7f2ff
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 128 deletions.
18 changes: 7 additions & 11 deletions src/BackgroundCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
namespace Andig;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;

class BackgroundCommand extends Command
{
Expand All @@ -31,16 +27,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$ftpDisabled = $this->config['fritzbox']['ftp']['disabled'] ?? false;
if (count($this->config['fritzbox']['fritzfons']) &&
$this->config['phonebook']['id'] == 0 && !$ftpDisabled) {
error_log('Downloading FRITZ!Box phonebook');
$xmlPhonebook = downloadPhonebook($this->config['fritzbox'], $this->config['phonebook']);
if (count($savedAttributes = uploadAttributes($xmlPhonebook, $this->config))) {
error_log('Numbers with special attributes saved' . PHP_EOL);
$output->writeln('<info>Downloading recent FRITZ!Box phonebook</info>');
$xmlPhonebook = downloadPhonebook($this->config['fritzbox'], $this->config['phonebook'], $output);
if (count($savedAttributes = uploadAttributes($xmlPhonebook, $this->config, $output))) {
$output->writeln('<info>Phone numbers with special attributes saved</info>');
} else { // no attributes are set in the FRITZ!Box or lost
$savedAttributes = downloadAttributes($this->config['fritzbox']); // try to get last saved attributes
$savedAttributes = downloadAttributes($this->config['fritzbox'], $output); // try to get last saved attributes
}
uploadBackgroundImage($savedAttributes, $this->config['fritzbox']);
uploadBackgroundImage($savedAttributes, $this->config['fritzbox'], $output);
} else {
error_log('No destination phones are defined and/or the first phone book is not selected!');
$output->writeln('<comment>No destination phones are defined and/or the first phone book is not selected!</comment>');
}

return 0;
Expand Down
10 changes: 7 additions & 3 deletions src/CardDav/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sabre\VObject\Reader;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Christian Putzke <[email protected]>
Expand Down Expand Up @@ -50,23 +51,26 @@ class Backend
*/
private $client;

/** @var OutputInterface */
private $output;

/**
* Constructor
* Sets the CardDAV server url
*
* @param string $url CardDAV server url
*/
public function __construct(string $url=null)
public function __construct(OutputInterface $output, string $url = null)
{
if ($url) {
$this->setUrl($url);
}

$this->setClientOptions([
'headers' => [
'Depth' => 1
]
]);
$this->output = $output;
}

/**
Expand Down Expand Up @@ -152,7 +156,7 @@ public function getVcards(): array
]);
} catch (RequestException $e) {
if ($e->hasResponse() && 404 == $e->getResponse()->getStatusCode()) {
error_log('Ignoring empty response from carddav REPORT request');
$this->output->writeln('<comment>Ignoring empty response from carddav REPORT request</comment>');
return [];
}

Expand Down
16 changes: 11 additions & 5 deletions src/CardDav/VcardFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Andig\CardDav\Backend;
use Sabre\VObject\Document;
use Sabre\VObject\Splitter\VCard;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @author Volker Püschel <[email protected]>
Expand All @@ -19,9 +20,9 @@ class VcardFile extends Backend
*/
private $fullpath;

public function __construct(string $fullpath = null)
public function __construct(OutputInterface $output, string $fullpath = null)
{
parent::__construct();
parent::__construct($output);
$this->fullpath = $fullpath;
}

Expand All @@ -36,12 +37,16 @@ public function getVcards(): array
return [];
}
if (!file_exists($this->fullpath)) {
error_log(sprintf('File %s not found!', $this->fullpath));
$error = sprintf('File %s not found!', $this->fullpath);
$this->output->writeln('<error>' . $error . '</error>');

return [];
}
$vcf = fopen($this->fullpath, 'r');
if (!$vcf) {
error_log(sprintf('File %s open failed!', $this->fullpath));
$error = sprintf('File %s open failed!', $this->fullpath);
$this->output->writeln('<error>' . $error . '</error>');

return [];
}
$cards = [];
Expand All @@ -50,7 +55,8 @@ public function getVcards(): array
if ($vCard instanceof Document) {
$cards[] = $this->enrichVcard($vCard);
} else {
error_log('Unexpected type: ' . get_class($vCard));
$error = 'Unexpected type: ' . get_class($vCard);
$this->output->writeln('<error>' . $error . '</error>');
}

$this->progress();
Expand Down
27 changes: 14 additions & 13 deletions src/ConvertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace Andig;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;

class ConvertCommand extends Command
{
Expand All @@ -35,47 +33,50 @@ protected function execute(InputInterface $input, OutputInterface $output)
$ftpDisabled = $this->config['fritzbox']['ftp']['disabled'] ?? false;
if ($ftpDisabled) {
$input->setOption('image', false);
error_log('Images can only be uploaded if ftp is enabled!');
$output->writeln('<comment>Images can only be uploaded if ftp is enabled!</comment>');
}

// we want to check for image upload show stoppers as early as possible
if ($input->getOption('image')) {
$this->checkUploadImagePreconditions($this->config['fritzbox'], $this->config['phonebook']);
}

error_log("Reading vCard(s) from file " . $filename);
$output->writeln('<info>Reading vCard(s) from file ' . $filename . '</info>');
$provider = localProvider($filename);
$vcards = $this->downloadProvider($output, $provider);
error_log(sprintf("\nRead %d vCard(s)", count($vcards)));
$info = sprintf("\nRead %d vCard(s)", count($vcards));
$output->writeln('<info>' . $info . '</info>');

// image upload
if ($input->getOption('image')) {
error_log("Detaching and uploading image(s)");
$output->writeln('<info>Detaching and uploading image(s)</info>');

$progress = new ProgressBar($output);
$progress = getProgressBar($output);
$progress->start(count($vcards));
$pictures = uploadImages($vcards, $this->config['fritzbox'], $this->config['phonebook'], function () use ($progress) {
$pictures = uploadImages($vcards, $this->config['fritzbox'], $this->config['phonebook'], $output, function () use ($progress) {
$progress->advance();
});
$progress->finish();

if ($pictures) {
error_log(sprintf(PHP_EOL . "Uploaded/refreshed %d of %d image file(s)", $pictures[0], $pictures[1]));
$info = sprintf(PHP_EOL . "Uploaded/refreshed %d of %d image file(s)", $pictures[0], $pictures[1]);
$output->writeln('<info>' . $info . '</info>');
}
}

// fritzbox format
$xmlPhonebook = exportPhonebook($vcards, $this->config);
error_log(sprintf(PHP_EOL."Converted %d vCard(s)", count($vcards)));
$xmlPhonebook = exportPhonebook($vcards, $this->config, $output);
$output->writeln('<info>' . sprintf(PHP_EOL."Converted %d vCard(s)", count($vcards)) . '</info>');

if (!count($vcards)) {
error_log("Phonebook empty - skipping write to file");
$output->writeln('<comment>Phonebook empty - skipping write to file!</comment>');
return 1;
}

$filename = $input->getArgument('destination');
if ($xmlPhonebook->asXML($filename)) {
error_log(sprintf("Succesfull saved phonebook as %s", $filename));
$info = sprintf("Succesfull saved phonebook as %s", $filename);
$output->writeln('<info>' . $info . '</info>');
}

return 0;
Expand Down
12 changes: 6 additions & 6 deletions src/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
namespace Andig;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;

class DownloadCommand extends Command
{
Expand All @@ -35,16 +33,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
// download from server or local files
$local = $input->getOption('local');
$vcards = $this->downloadAllProviders($output, $input->getOption('image'), $local);
error_log(sprintf("Downloaded %d vCard(s) in total", count($vcards)));
$info = sprintf("Downloaded %d vCard(s) in total", count($vcards));
$output->writeln('<info>' . $info . '</info>');

// dissolve
if ($input->getOption('dissolve')) {
$vcards = $this->processGroups($vcards);
$vcards = $this->processGroups($vcards, $output);
}

// filter
if ($input->getOption('filter')) {
$vcards = $this->processFilters($vcards);
$vcards = $this->processFilters($vcards, $output);
}

// save to file
Expand All @@ -55,7 +54,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

$filename = $input->getArgument('filename');
if (file_put_contents($filename, $vCardContents) != false) {
error_log(sprintf("Succesfully saved vCard(s) in %s", $filename));
$info = sprintf("Succesfully saved vCard(s) in %s", $filename);
$output->writeln('<info>' . $info . '</info>');
}

return 0;
Expand Down
48 changes: 34 additions & 14 deletions src/DownloadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@

trait DownloadTrait
{
/**
* get progress bar in info scheme (green)
*
* @param OutputInterface $output
* @return ProgressBar
*/
public function getProgressBar(OutputInterface $output): ProgressBar
{
$progress = new ProgressBar($output);
$progress->setFormatDefinition('info', '<info>%current% [%bar%]</info>');
$progress->setFormat('info');

return $progress;
}

/**
* Default list of card attributes to substitute
*
Expand All @@ -28,7 +43,7 @@ public function getDefaultSubstitutes(): array
*/
public function downloadProvider(OutputInterface $output, Backend $provider): array
{
$progress = new ProgressBar($output);
$progress = self::getProgressBar($output);
$progress->start();
$cards = download($provider, function () use ($progress) {
$progress->advance();
Expand All @@ -50,26 +65,28 @@ public function downloadAllProviders(OutputInterface $output, bool $downloadImag
$vcards = [];

foreach ($local as $file) {
error_log("Reading vCard(s) from file ".$file);
$output->writeln('<info>Reading vCard(s) from file '. $file . '</info>');

$provider = localProvider($file);
$cards = $this->downloadProvider($output, $provider);

error_log(sprintf("\nRead %d vCard(s)", count($cards)));
$info = sprintf("\nRead %d vCard(s)", count($cards));
$output->writeln('<info>' . $info . '</info>');
$vcards = array_merge($vcards, $cards);
}

foreach ($this->config['server'] as $server) {
error_log("Downloading vCard(s) from account ".$server['user']);
$output->writeln('<info>Downloading vCard(s) from account ' . $server['user'] . '</info>');

$provider = backendProvider($server);
$provider = backendProvider($server, $output);
if ($downloadImages) {
$substitutes = $this->getDefaultSubstitutes();
$provider->setSubstitutes($substitutes);
}
$cards = $this->downloadProvider($output, $provider);

error_log(sprintf("\nDownloaded %d vCard(s)", count($cards)));
$info = sprintf("\nDownloaded %d vCard(s)", count($cards));
$output->writeln('<info>' . $info . '</info>');
$vcards = array_merge($vcards, $cards);
}

Expand All @@ -82,13 +99,13 @@ public function downloadAllProviders(OutputInterface $output, bool $downloadImag
* @param mixed[] $vcards
* @return mixed[]
*/
public function processGroups(array $vcards): array
public function processGroups(array $vcards, $output): array
{
$quantity = count($vcards);

error_log("Dissolving groups (e.g. iCloud)");
$output->writeln('<info>Dissolving groups (e.g. iCloud)</info>');
$vcards = dissolveGroups($vcards);
error_log(sprintf("Dissolved %d group(s)", $quantity - count($vcards)));
$info = sprintf("Dissolved %d group(s)", $quantity - count($vcards));
$output->writeln('<info>' . $info . '</info>');

return $vcards;
}
Expand All @@ -97,15 +114,18 @@ public function processGroups(array $vcards): array
* Filter included/excluded vcards
*
* @param mixed[] $vcards
* @param OutputInterface $output
* @return mixed[]
*/
public function processFilters(array $vcards): array
public function processFilters(array $vcards, OutputInterface $output): array
{
$quantity = count($vcards);

error_log(sprintf("Filtering %d vCard(s)", $quantity));
$vcards = filter($vcards, $this->config['filters']);
error_log(sprintf("Filtered %d vCard(s)", $quantity - count($vcards)));
$info = sprintf("Filtering %d vCard(s)", $quantity);
$output->writeln('<info>' . $info . '</info>');
$vcards = filter($vcards, $this->config['filters'], $output);
$info = sprintf("Filtered %d vCard(s)", $quantity - count($vcards));
$output->writeln('<info>' . $info . '</info>');

return $vcards;
}
Expand Down
14 changes: 10 additions & 4 deletions src/FritzBox/BackgroundImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Andig\FritzBox;

use Andig\FritzBox\Api;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Copyright (c) 2019 Volker Püschel
Expand All @@ -20,8 +21,12 @@ class BackgroundImage
/** @var int */
protected $textColor;

public function __construct()
/** @var OutputInterface */
private $output;

public function __construct(OutputInterface $output)
{
$this->output = $output;
$this->bgImage = $this->getImageAsset(dirname(__DIR__, 2) . '/assets/keypad.jpg');
putenv('GDFONTPATH=' . realpath('.'));
$this->setFont(dirname(__DIR__, 2) . '/assets/impact.ttf');
Expand Down Expand Up @@ -212,15 +217,16 @@ public function uploadImage($quickdials, $config)
continue;
}

error_log(sprintf("Uploading background image to FRITZ!Fon #%s", $phone));
$info = sprintf("Uploading background image to FRITZ!Fon #%s", $phone);
$this->output->writeln('<info>' . $info . '</info>');

$body = $this->getBody($fritz->getSID(), $phone, $backgroundImage);
$result = $fritz->postImage($body);
if (strpos($result, 'Das Bild wurde erfolgreich hinzugefügt') ||
strpos($result, 'The image was added successfully')) {
error_log('Background image upload successful');
$this->output->writeln('<info>Background image upload successful</info>');
} else {
error_log('Background image upload failed');
$this->output->writeln('<comment>Background image upload failed</comment>');
}
}
}
Expand Down
Loading

0 comments on commit bf7f2ff

Please sign in to comment.