Skip to content

Commit

Permalink
Code Coverage (#8)
Browse files Browse the repository at this point in the history
Code Coverage
  • Loading branch information
kdebisschop authored Dec 27, 2020
1 parent 9f7e1ae commit 9bd75ae
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 84 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ jobs:
with:
fetch-depth: 0

- name: 'Get Previous tag'
id: previoustag
uses: "WyriHaximus/github-action-get-previous-tag@master"

- name: Setup PHP
id: php-setup
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -69,6 +73,9 @@ jobs:
- name: SonarCloud Scan
if: ${{ matrix.php-version == '7.4' }}
uses: SonarSource/sonarcloud-github-action@master
with:
args: >
-Dsonar.projectVersion=${{ steps.previoustag.outputs.tag }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
42 changes: 22 additions & 20 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,31 @@ $finder = PhpCsFixer\Finder::create()
->in(__DIR__);

$customRules = [
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'declare_strict_types' => true,
'phpdoc_summary' => false,
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'declare_strict_types' => true,
'new_with_braces' => false,
'phpdoc_summary' => false,
];

$psr12Rules = [
'@PSR2' => true,
'blank_line_after_opening_tag' => true,
'braces' => ['allow_single_line_closure' => true],
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'function_typehint_space' => true,
'new_with_braces' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'no_empty_statement' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_whitespace_in_blank_line' => true,
'return_type_declaration' => ['space_before' => 'none'],
'single_trait_insert_per_statement' => true,
'@PSR2' => true,
'blank_line_after_opening_tag' => true,
'braces' => ['allow_single_line_closure' => true],
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'function_typehint_space' => true,
'new_with_braces' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'no_empty_statement' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_whitespace_in_blank_line' => true,
'return_type_declaration' => ['space_before' => 'none'],
'single_space_after_construct' => false,
'single_trait_insert_per_statement' => true,
];

$config = new PhpCsFixer\Config();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"scripts": {
"fix": "php-cs-fixer fix -v",
"fixer": "php-cs-fixer fix --dry-run -v",
"fixer": "php-cs-fixer fix --dry-run --using-cache=no -v",
"cs": "phpcs -p -s",
"analyze": "phpstan --memory-limit=256M analyze",
"test": "phpunit",
Expand Down
73 changes: 30 additions & 43 deletions src/Extractors/GoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class GoogleAnalytics extends Extractor
{
private const REPORT_PAGE_SIZE = 1000;

/** @var array */
/**
* @var array
*/
protected $availableOptions = ['startDate', 'endDate', 'views', 'properties', 'dimensions', 'metrics'];

/**
Expand Down Expand Up @@ -140,26 +142,34 @@ class GoogleAnalytics extends Extractor
* If specified, properties will limit the specific GA properties to extract.
*
* ```php
* $options = ['properties' => ['All data']];
* $options = ['properties' => ['www.example.com']];
* ```
*
* @var string[]
*/
protected array $properties = [];

private \Google_Service_Analytics $analyticsService;
protected \Closure $delayFunction;

/** @var string[] */
/**
* @var string[]
*/
private array $dimensionHeaders;

/** @var string[] */
/**
* @var string[]
*/
private array $metricHeaders;

private \Google_Service_AnalyticsReporting $reportingService;
private int $clientReqCount = 0;

private \Google_Service_AnalyticsReporting_ReportRequest $reportRequest;
protected int $oneSecond = 1000000;

private int $clientReqCount = 0;
protected \Google_Service_Analytics $analyticsService;

protected \Google_Service_AnalyticsReporting $reportingService;

protected \Google_Service_AnalyticsReporting_ReportRequest $reportRequest;

/**
* Creates a new Google Analytics Extractor instance.
Expand Down Expand Up @@ -194,6 +204,13 @@ public function options(array $options): Step
parent::options($options);
$this->validate();

if (!isset($this->delayFunction)) {
// Delay 1 second to avoid 100 requests per 100 seconds quota exceeded.
$this->delayFunction = function (): void {
usleep($this->oneSecond);
};
}

return $this;
}

Expand All @@ -214,7 +231,10 @@ public function extract(): \Generator

$request = $this->reportRequest($profileSummary->getId());
$response = $this->reportingService->reports->batchGet($request);
/** @var \Google_Service_AnalyticsReporting_Report $report */

/**
* @var \Google_Service_AnalyticsReporting_Report $report
*/
foreach ($response as $report) {
$this->setHeaders($report);
$rows = $report->getData()->getRows();
Expand All @@ -228,36 +248,6 @@ public function extract(): \Generator
}
}

/**
* Enables dependency injection for the analytics service.
*/
public function setAnalyticsSvc(\Google_Service_Analytics $analyticsService): self
{
$this->analyticsService = $analyticsService;

return $this;
}

/**
* Enables dependency injection for the analytics reporting service.
*/
public function setReportingSvc(\Google_Service_AnalyticsReporting $reportingService): self
{
$this->reportingService = $reportingService;

return $this;
}

/**
* Enables dependency injection for the analytics reporting service.
*/
public function setReportRequest(\Google_Service_AnalyticsReporting_ReportRequest $reportRequest): self
{
$this->reportRequest = $reportRequest;

return $this;
}

/**
* Delay requests to avoid exceeding Google quotas.
*
Expand All @@ -274,11 +264,8 @@ public function setReportRequest(\Google_Service_AnalyticsReporting_ReportReques
private function delay(): void
{
if ($this->clientReqCount >= 100) {
// Delay 1 second plus or minus a random jitter to avoid 100 requests per 100 seconds quota exceeded.
$delay = 1 + (mt_rand() / mt_getrandmax() - 0.5);
usleep((int) (1000000 * $delay));
call_user_func($this->delayFunction);
}

++$this->clientReqCount;
}

Expand Down
Loading

0 comments on commit 9bd75ae

Please sign in to comment.