Skip to content

Commit

Permalink
Use interface for item (#4)
Browse files Browse the repository at this point in the history
* Updated item to use ItemInterface

* Fix style

* Update readme

* added more ignore path

* fixes from php cs-fixer

* added cs fixer

* Update config

* update from cs fixer

* Styles and files

* Added new rules

* Added ItemInterface

* Apply diff from styleCI

* More fixes

* Fix style

* Update types

* Fix docblock
  • Loading branch information
cjonstrup authored Jan 25, 2019
1 parent 828bd5b commit d6aba2e
Show file tree
Hide file tree
Showing 15 changed files with 368 additions and 163 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.phar
composer.lock
priv-test.php
.idea
.php_cs.cache
30 changes: 11 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ $basket = new Basket(new Session, new Cookie);
Inserting an item into the basket is easy. The required keys are id, name, price, weight and quantity, although you can pass
over any custom data that you like.
```php
$basket->insert([
$basket->insert(new Item([
'id' => 'foo',
'name' => 'bar',
'price' => 100,
'quantity' => 2,
'weight' => 300
]);
]));

```

### Inserting items with options into the basket
Inserting an item into the basket is easy. The required keys are id, name, price and quantity, although you can pass
over any custom data that you like. If option items contains price or weight there values are added to the total weight / price of the product.
```php
$basket->insert([
$basket->insert(new Item([
'id' => 'foo',
'name' => 'bar',
'price' => 100,
Expand All @@ -63,7 +63,7 @@ $basket->insert([
'price' => 10
],
],
]);
]));
```

### Setting the tax rate for an item
Expand All @@ -73,14 +73,14 @@ the price of the item.
In the below example we will use 25% for the tax rate.

```php
$basket->insert([
$basket->insert(new Item([
'id' => 'mouseid',
'name' => 'Mouse',
'price' => 100,
'quantity' => 1,
'tax' => 25,
'weight' => 200
]);
]));
```

### Updating items in the basket
Expand All @@ -93,14 +93,6 @@ foreach ($basket->contents() as $item) {
}
```

### Removing basket items
You can remove any items in your basket by using the ```remove()``` method on any basket item.
```php
foreach ($basket->contents() as $item) {
$item->remove();
}
```

### Destroying/emptying the basket
You can completely empty/destroy the basket by using the ```destroy()``` method.
```php
Expand Down Expand Up @@ -140,6 +132,11 @@ $basket->total(false);
$basket->has($itemIdentifier);
```

### Remove an item from the Basket
```php
$basket->remove($identifier)
```

### Retrieve an item object by identifier
```php
$basket->item($itemIdentifier);
Expand All @@ -159,11 +156,6 @@ foreach ($basket->contents() as $item) {
}
```

### Remove an item from the Basket
```php
$item->remove();
```

### You can also get the total weight for a single item
```php
$item->weight();
Expand Down
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '2'

services: # sorted alphabetically
csfixer:
build: ./docker/csfixer
image: docker.lenius.dk/csfixer
27 changes: 27 additions & 0 deletions docker/csfixer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM php:7.2-fpm

RUN apt-get update && \
apt-get install git libpq-dev libldap2-dev zlib1g-dev libcurl4-gnutls-dev libicu-dev libmcrypt-dev libxml2-dev g++ -y && \
rm -rf /var/lib/apt/lists/* && \
docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && \
docker-php-ext-install -j$(nproc) zip ldap intl curl pdo pdo_mysql pdo_pgsql pgsql bcmath soap zip sockets && \
pecl install redis apcu && \
docker-php-ext-enable redis apcu

# Unzip is required for composer.
RUN apt-get update && apt-get install -y unzip

# Install composer
ADD https://getcomposer.org/installer /composer-setup.php

RUN php /composer-setup.php \
--install-dir=/usr/local/bin/ \
--filename=composer \
&& rm /composer-setup.php

# Allow Composer to be run as root.
ENV COMPOSER_ALLOW_SUPERUSER 1

ENV PATH="~/.composer/vendor/bin:${PATH}"

RUN composer global require friendsofphp/php-cs-fixer
81 changes: 81 additions & 0 deletions fix_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash -e

# Directory or file - Should be relative path
PROJECT_PATH="${1}"

# Default
if [[ ${PROJECT_PATH} = "" ]]; then

DEFAULT_DIRS=(src tests)

echo "Running $0 on default directories: ${DEFAULT_DIRS[@]}"

for dir_to_check in "${DEFAULT_DIRS[@]}"; do
$0 "${dir_to_check}"
done

exit 0
fi

# Sanity check
if [[ ! -e ${PROJECT_PATH} ]]; then
echo "Doesn't seem to exist: ${PROJECT_PATH}" >&2
exit 1
fi

PROJECT_DIR=${PROJECT_PATH}

if [[ -f ${PROJECT_PATH} ]]; then
PROJECT_DIR=$(dirname ${PROJECT_PATH})
fi

echo "Fixing: ${PROJECT_PATH}"

docker run \
--rm \
-v "$(pwd)/${PROJECT_DIR}":/source/${PROJECT_DIR} \
docker.lenius.dk/csfixer \
/root/.composer/vendor/bin/php-cs-fixer \
--verbose \
--using-cache=yes \
--cache-file=/source/${PROJECT_DIR}/.php_cs.cache \
--rules='
{
"@PSR2": true,
"array_indentation": true,
"binary_operator_spaces": {
"operators": {"=>": "align_single_space"}
},
"blank_line_after_opening_tag": false,
"blank_line_before_statement": {"statements": ["do", "for", "foreach", "if", "switch", "throw", "while", "return"]},
"concat_space": {"spacing": "one"},
"no_blank_lines_after_phpdoc": false,
"no_blank_lines_after_class_opening": true,
"no_whitespace_in_blank_line": true,
"no_trailing_comma_in_singleline_array": true,
"no_trailing_comma_in_list_call": true,
"no_extra_blank_lines": true,
"no_empty_phpdoc": true,
"no_unused_imports": true,
"method_argument_space": true,
"method_separation": true,
"phpdoc_indent": true,
"phpdoc_scalar": true,
"phpdoc_order": true,
"phpdoc_align": true,
"phpdoc_separation": true,
"phpdoc_inline_tag": true,
"phpdoc_no_access": true,
"phpdoc_no_package": true,
"phpdoc_separation": true,
"phpdoc_summary": true,
"phpdoc_to_comment": true,
"phpdoc_trim": true,
"phpdoc_order": true,
"single_quote": {"strings_containing_single_quote_chars": true},
"whitespace_after_comma_in_array": true,
"concat_space": {
"spacing": "none"
}
}' \
fix /source/${PROJECT_PATH}
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<directory>./vendor</directory>
<file>./src/IdentifierInterface.php</file>
<file>./src/StorageInterface.php</file>
<file>./src/ItemInterface.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
45 changes: 28 additions & 17 deletions src/Basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@
*/
class Basket
{
/** @var string $id */
protected $id;

/** @var IdentifierInterface $identifier */
protected $identifier;

/** @var StorageInterface $store */
protected $store;

/** @var array $requiredParams */
protected $requiredParams = [
'id',
'name',
Expand Down Expand Up @@ -78,24 +83,26 @@ public function &contents($asArray = false)
/**
* Insert an item into the basket.
*
* @param array $item An array of item data
* @param ItemInterface $item
*
* @return string A unique item identifier
*/
public function insert(array $item)
public function insert(ItemInterface $item)
{
$this->checkArgs($item);

$itemIdentifier = $this->createItemIdentifier($item);

if ($this->has($itemIdentifier) && $this->item($itemIdentifier) instanceof Item) {
$item['quantity'] = $this->item($itemIdentifier)->quantity + $item['quantity'];
if ($this->has($itemIdentifier) && $this->item($itemIdentifier) instanceof ItemInterface) {
$this->item($itemIdentifier)->quantity;

$item->quantity = $this->item($itemIdentifier)->quantity + $item->quantity;
$this->update($itemIdentifier, $item);

return $itemIdentifier;
}

$item = new Item($itemIdentifier, $item, $this->store);
$item->setIdentifier($itemIdentifier);

$this->store->insertUpdate($item);

Expand All @@ -105,9 +112,9 @@ public function insert(array $item)
/**
* Update an item.
*
* @param string $itemIdentifier The unique item identifier
* @param string|array $key The key to update, or an array of key-value pairs
* @param mixed $value The value to set $key to
* @param string $itemIdentifier The unique item identifier
* @param mixed $key The key to update, or an array of key-value pairs
* @param mixed $value The value to set $key to
*
* @return void
*/
Expand Down Expand Up @@ -269,32 +276,36 @@ public function setIdentifier($identifier)
/**
* Create a unique item identifier.
*
* @param array $item An array of item data
* @param ItemInterface $item
*
* @return string An md5 hash of item
*/
protected function createItemIdentifier(array $item)
protected function createItemIdentifier(ItemInterface $item)
{
if (!array_key_exists('options', $item)) {
$item['options'] = [];
if (!array_key_exists('options', $item->toArray())) {
$item->options = [];
}

ksort($item['options']);
$options = $item->options;

ksort($options);

$item->options = $options;

return md5($item['id'].serialize($item['options']));
return md5($item->id.serialize($item->options));
}

/**
* Check if a basket item has the required parameters.
*
* @param array $item An array of item data
* @param ItemInterface $item
*
* @return void
*/
protected function checkArgs(array $item)
protected function checkArgs(ItemInterface $item)
{
foreach ($this->requiredParams as $param) {
if (!array_key_exists($param, $item)) {
if (!array_key_exists($param, $item->toArray())) {
throw new InvalidArgumentException("The '{$param}' field is required");
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Identifier/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
class Runtime implements IdentifierInterface
{
/** @var string $identifier */
protected static $identifier;

/**
Expand Down
Loading

0 comments on commit d6aba2e

Please sign in to comment.