-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
210 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
/** | ||
* Librairie du pooler de stockage du token Tuya via le système de fichier | ||
* | ||
* @author Olivier <[email protected]> | ||
* | ||
* @package TuyaCloudApi | ||
*/ | ||
|
||
namespace Sabinus\TuyaCloudApi\Tools; | ||
|
||
use Symfony\Component\Filesystem\Filesystem; | ||
use Symfony\Component\Filesystem\Exception\FileNotFoundException; | ||
use Symfony\Component\Filesystem\Exception\IOException; | ||
|
||
|
||
class TokenPool | ||
{ | ||
|
||
const TOKEN_FILE = 'tuya.token'; | ||
|
||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
|
||
/** | ||
* Chemin contenant le fichier du token | ||
* | ||
* @var String | ||
*/ | ||
private $folder; | ||
|
||
|
||
/** | ||
* Constructeur | ||
* | ||
* @param String $folder | ||
*/ | ||
public function __construct($folder = null) | ||
{ | ||
$this->folder = (empty($folder)) ? sys_get_temp_dir() : $folder; | ||
|
||
$this->filesystem = new FileSystem(); | ||
} | ||
|
||
|
||
/** | ||
* Change l'emplacement du dossier | ||
* | ||
* @param String $folder | ||
*/ | ||
public function setFolder($folder) | ||
{ | ||
$this->folder = $folder; | ||
} | ||
|
||
|
||
/** | ||
* Retourne la valeur du token sauvegardé depuis le système de fichier | ||
* | ||
* @return Token | ||
*/ | ||
public function fetchTokenFromCache() | ||
{ | ||
$file = $this->getFilePath(); | ||
|
||
try { | ||
$token = @unserialize(@file_get_contents($file)); | ||
if ($token === false) { | ||
return null; | ||
} | ||
} catch (FileNotFoundException $e) { | ||
return null; | ||
} | ||
|
||
return $token; | ||
} | ||
|
||
|
||
/** | ||
* Enregistre le token sur le système de fichier | ||
* | ||
* @param Token $token | ||
*/ | ||
public function storeTokenInCache($token) | ||
{ | ||
$data = serialize($token); | ||
$file = $this->getFilePath(); | ||
|
||
if (false === @file_put_contents($file, $data)) { | ||
throw new IOException(sprintf('Failed to write file "%s".', $file), 0, null, $file); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Efface le fichier du token | ||
*/ | ||
public function clearFromCache() | ||
{ | ||
try { | ||
$this->filesystem->remove($this->getFilePath()); | ||
} catch (FileNotFoundException $e) { | ||
return true; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Retourne le chemin complet du fichier | ||
* | ||
* @return String | ||
*/ | ||
private function getFilePath() | ||
{ | ||
return sprintf('%s/%s', $this->folder, self::TOKEN_FILE); | ||
} | ||
|
||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Test de la class TokenPool | ||
* | ||
* @author Olivier <[email protected]> | ||
* | ||
* @package TuyaCloudApi | ||
*/ | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sabinus\TuyaCloudApi\Tools\TokenPool; | ||
|
||
|
||
class TokenPoolTest extends TestCase | ||
{ | ||
|
||
public function testStoreToken() | ||
{ | ||
$pool = new TokenPool(); | ||
$this->assertSame(null, $pool->fetchTokenFromCache()); | ||
$pool->storeTokenInCache([0,1,2]); | ||
$this->assertSame([0,1,2], $pool->fetchTokenFromCache()); | ||
$pool->clearFromCache(); | ||
$this->assertSame(null, $pool->fetchTokenFromCache()); | ||
} | ||
|
||
|
||
public function testStoreOtherFolderToken() | ||
{ | ||
$pool = new TokenPool(); | ||
$pool->setFolder('/var/tmp'); | ||
$pool->storeTokenInCache([1,2,3]); | ||
$this->assertSame([1,2,3], $pool->fetchTokenFromCache()); | ||
$pool->clearFromCache(); | ||
$this->assertSame(null, $pool->fetchTokenFromCache()); | ||
} | ||
|
||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters