Completed
Push — master ( 19c355...d500cb )
by Guillermo A.
02:33
created

ScanOperation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Guillermoandrae\DynamoDb\Operation;
4
5
use Aws\DynamoDb\DynamoDbClient;
6
use Aws\DynamoDb\Exception\DynamoDbException;
7
use Aws\DynamoDb\Marshaler;
8
use Guillermoandrae\Common\Collection;
9
use Guillermoandrae\Common\CollectionInterface;
10
use Guillermoandrae\DynamoDb\Contract\AbstractSearchOperation;
11
use Guillermoandrae\DynamoDb\Exception;
12
13
/**
14
 * @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#scan
15
 */
16
final class ScanOperation extends AbstractSearchOperation
17
{
18
    /**
19
     * Registers the DynamoDb client, Marshaler, and table name with this object.
20
     *
21
     * @param DynamoDbClient $client The DynamoDb client.
22
     * @param Marshaler $marshaler The Marshaler.
23
     * @param string $tableName The table name.
24
     */
25 3
    public function __construct(DynamoDbClient $client, Marshaler $marshaler, string $tableName)
26
    {
27 3
        $this->setClient($client);
28 3
        $this->setMarshaler($marshaler);
29 3
        $this->setTableName($tableName);
30 3
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 3
    public function execute(): CollectionInterface
36
    {
37
        try {
38 3
            $results = $this->client->scan($this->toArray());
39 2
            $rows = [];
40 2
            foreach ($results['Items'] as $item) {
41 1
                $rows[] = $this->marshaler->unmarshalItem($item);
42
            }
43 2
            return Collection::make($rows);
44 1
        } catch (DynamoDbException $ex) {
45 1
            throw new Exception($ex->getMessage());
46
        }
47
    }
48
}
49