Skip to content

Commit

Permalink
Add downloader factory (#3)
Browse files Browse the repository at this point in the history
* add fonts factry

* Remove unnecessary imports.

Co-authored-by: jseparovic1 <jurica.separovic@gmail.com>
  • Loading branch information
jseparovic1 and jseparovic1 authored Apr 24, 2020
1 parent c61b7fe commit 2d60704
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
21 changes: 6 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,20 @@ This package will download specified font from self hosted google fonts reposito
### Basic usage

```PHP
$client = new GuzzleHttp\Client;
$filesystemAdapter = new League\Flysystem\Adapter\Local('web/fonts');
$filesystem = new League\Flysystem\Filesystem($filesystemAdapter);
use PCode\GoogleFontDownloader\Lib\DownloaderFactory;
use PCode\GoogleFontDownloader\Lib\FontExtension;

$fileService = new FileService($filesystem);
$fontService = new FontService($fileService, 'fonts/');
$downloadService = new DownloadService($client, $fileService, $fontService);
$uri = new Uri();
$api = new MajodevAPI($fontService, $downloadService, $uri);
//Create using factory
$downloader = DownloaderFactory::create(__DIR__ . '/fonts');

$downloader = new Downloader($fileService, $fontService, $downloadService, $api);
```

### Then use it like
```PHP
//Download specific version of font
$downloadedFont = $downloader->download("Open Sans", "v12");

//Download font latest version
$downloadedFont = $downloader->downloadLatest("Open Sans");

//You can also provide font extension, use constan from PCode\GoogleFontDownloader\Lib\FontExtension class
//Default font extension is WOFF"
//You can also provide font extension, use constant from PCode\GoogleFontDownloader\Lib\FontExtension
//Default font extension is WOFF
$downloadedFont = $downloader->downloadLatest("Open Sans", FontExtension::WOFF22);

//Check if font can be downloaded
Expand Down
37 changes: 37 additions & 0 deletions src/Lib/DownloaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace PCode\GoogleFontDownloader\Lib;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use PCode\GoogleFontDownloader\Interfaces\DownloaderInterface;
use PCode\GoogleFontDownloader\Lib\Service\DownloadService;
use PCode\GoogleFontDownloader\Lib\Service\FileService;
use PCode\GoogleFontDownloader\Lib\Service\FontService;

class DownloaderFactory
{
/**
* @param string $path
* @return DownloaderInterface
*/
public static function create($path)
{
$fileService = new FileService(
new Filesystem(new Local($path))
);

$fontService = new FontService($fileService, basename($path) . '/');

$downloadService = new DownloadService(new Client(), $fileService, $fontService);

return new Downloader(
$fileService,
$fontService,
$downloadService,
new MajodevAPI($fontService, $downloadService, new Uri())
);
}
}
28 changes: 5 additions & 23 deletions tests/FontDownloaderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,19 @@

namespace Pcode\GoogleFontDownloader;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Uri;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use PCode\GoogleFontDownloader\Lib\Downloader;
use PCode\GoogleFontDownloader\Lib\MajodevAPI;
use PCode\GoogleFontDownloader\Lib\Service\DownloadService;
use PCode\GoogleFontDownloader\Lib\Service\FileService;
use PCode\GoogleFontDownloader\Lib\Service\FontService;
use PCode\GoogleFontDownloader\Interfaces\DownloaderInterface;
use PCode\GoogleFontDownloader\Lib\DownloaderFactory;
use PHPUnit\Framework\TestCase;

class FontDownloaderTestCase extends TestCase
{
const DATA_DIRECTORY = './tests/data/';

const FONTS_DIR = 'fonts/';
const FONTS_DIR = __DIR__ . '/data/fonts';

/**
* @return Downloader
* @return DownloaderInterface
*/
public function getDownloader()
{
$filesystemAdapter = new Local(self::DATA_DIRECTORY.self::FONTS_DIR);
$filesystem = new Filesystem($filesystemAdapter);

$fileService = new FileService($filesystem);
$fontService = new FontService($fileService,self::FONTS_DIR);

$downloadService = new DownloadService(new Client(), $fileService, $fontService);
$api = new MajodevAPI($fontService, $downloadService, new Uri());

return new Downloader($fileService, $fontService, $downloadService, $api);
return DownloaderFactory::create(self::FONTS_DIR);
}
}

0 comments on commit 2d60704

Please sign in to comment.