Created
April 12, 2019 14:51
-
-
Save PurpleBooth/9053c9080654256363eed1480c0a7376 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace Armakuni\Demo\PhpCounter; | |
use Google\Cloud\Datastore\DatastoreClient; | |
class CounterService | |
{ | |
private $datastore; | |
/** | |
* CounterService constructor. | |
* @param DatastoreClient $datastore | |
*/ | |
public function __construct(DatastoreClient $datastore) | |
{ | |
$this->datastore = $datastore; | |
} | |
/** | |
* @return int | |
*/ | |
public function incrementCounter(): int | |
{ | |
$this->addOne(); | |
return $this->getCount(); | |
} | |
private function addOne(): void | |
{ | |
$counterKey = $this->datastore->key('Counter'); | |
$counterKey = $this->datastore->allocateId($counterKey); | |
$counter = $this->datastore->entity($counterKey, []); | |
$this->datastore->upsert($counter); | |
} | |
/** | |
* @return int | |
*/ | |
private function getCount(): int | |
{ | |
$query = $this->datastore->query() | |
->keysOnly(); | |
$results = $this->datastore->runQuery($query); | |
return iterator_count($results); | |
} | |
} |
This file contains hidden or 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
<?php | |
$count = Tracer::inSpan( | |
['name' => 'count'], | |
function () use ($datastore) { | |
return (new CounterService($datastore))->incrementCounter(); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment