Skip to content

Commit

Permalink
Dodgy stuff! Investigate further
Browse files Browse the repository at this point in the history
  • Loading branch information
johanib committed Dec 2, 2024
1 parent 54cdd82 commit d7b7d89
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/packages/framework.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ when@test:
test: true
session:
storage_factory_id: session.storage.factory.mock_file
handler_id: null
cookie_secure: true
cookie_samesite: none
name: MOCKSESSID
cookie_httponly: true
profiler:
collect: false
8 changes: 7 additions & 1 deletion config/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ services:
arguments:
- '/^Behat UA$/'

# surfnet_gssp.value_store.service:
# class: Surfnet\GsspBundle\Service\ValueStore\InMemoryValueStore
# public: true

surfnet_gssp.value_store.service:
class: Surfnet\GsspBundle\Service\ValueStore\InMemoryValueStore
class: Surfnet\Tiqr\Service\FileValueStore
public: true
arguments:
$filePath: '/var/www/html/var/store.json'
100 changes: 100 additions & 0 deletions src/Service/FileValueStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types = 1);

/**
* Copyright 2018 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\Tiqr\Service;

use InvalidArgumentException;
use Surfnet\GsspBundle\Exception\NotFound;
use Surfnet\GsspBundle\Service\ValueStore;

final class FileValueStore implements ValueStore
{
private string $filePath;

public function __construct(string $filePath)
{
$this->filePath = $filePath;
if (!file_exists($this->filePath)) {
file_put_contents($this->filePath, json_encode([]));
chmod($this->filePath, 0666);
}
}

/**
* @return array<string,mixed>
*/
private function readValues(): array
{
$content = file_get_contents($this->filePath);
if ($content === false) {
throw new InvalidArgumentException(sprintf('Could not read FileValueStore storage file. %s', $this->filePath));
}

$result = json_decode($content, true);

return is_array($result) ? $result : [];
}

/**
* @param array<string,mixed> $values
*/
private function writeValues(array $values): void
{
file_put_contents($this->filePath, json_encode($values));
}

public function set(string $key, mixed $value): self
{
$values = $this->readValues();
$values[$key] = $value;
$this->writeValues($values);
return $this;
}

public function get(string $key): mixed
{
$values = $this->readValues();
if (!isset($values[$key])) {
throw NotFound::stateProperty($key);
}
return $values[$key];
}

/**
* @SuppressWarnings(PHPMD.ShortMethodName)
*/
public function is(string $key, mixed $value): bool
{
$values = $this->readValues();
return isset($values[$key]) && $values[$key] === $value;
}

public function has(string $key): bool
{
$values = $this->readValues();
return isset($values[$key]);
}

public function clear(): self
{
$this->writeValues([]);
return $this;
}
}

0 comments on commit d7b7d89

Please sign in to comment.