<commit_before># CookiesBundle

Cookies bundle provides Symfony way to handle cookies defining them as services.

[![Build Status](https://travis-ci.org/ongr-io/CookiesBundle.svg?branch=master)](https://travis-ci.org/ongr-io/CookiesBundle)
[![Coverage Status](https://coveralls.io/repos/ongr-io/CookiesBundle/badge.svg?branch=master&service=github)](https://coveralls.io/github/ongr-io/CookiesBundle?branch=master)
[![Latest Stable Version](https://poser.pugx.org/ongr/cookies-bundle/v/stable)](https://packagist.org/packages/ongr/cookies-bundle)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ongr-io/CookiesBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ongr-io/CookiesBundle/?branch=master)

## Install bundle
   
To install this bundle add it to composer.

```bash

   composer require ongr/cookies-bundle

```

Then register it in `AppKernel.php`

```php

   class AppKernel extends Kernel
   {
       public function registerBundles()
       {
           return [
               // ...
               new ONGR\CookiesBundle\ONGRCookiesBundle(),
           ];
       }

       // ...
   }

```

That's it - the bundle is ready for work.


## Simple usage example

```php

    class CookieController
    {
        public function readAction()
        {
            $cart = $this->container->get('project.cookie.cart')->getValue();
            $items = $cartCookie['items'];
            // ...
        }
    }
```

Cookie configuration example:

```yaml

    parameters:
        # One can optionally override defaults.
        project.cookie.cart.defaults:
            http_only: false
            expires_interval: P5DT4H # 5 days and 4 hours
    
    services:
        project.cookie.cart:
            class: %ongr_cookie.json.class%
            arguments: [ "project_cart" ]
            calls:
                - [ setDefaults, [ %project.cookie_foo.defaults% ] ]
            tags:
                - { name: ongr_cookie.cookie }
```

## Working with cookies

### How to define a cookie model

ONGR provides cookie model abstraction for working with cookie values in the request and response.

One can define a service:

```yaml

    parameters:
        project.cookie.foo.name: cookie.foo
        project.cookie.foo.defaults: # Defaults section is optional
            http_only: false
            expires_interval: P5DT4H # 5 days and 4 hours

    services:
        project.cookie.foo:
            class: %ongr_cookie.json.class%
            arguments: [ %project.cookie.foo.name% ]
            calls:
                - [setDefaults, [%project.cookie.foo.defaults%]] # Optional
            tags:
                - { name: ongr_cookie.cookie }

```

Such injected service allows accessing cookie value. If the value has been modified by your code, it will send new value back to the client browser.

```php

    class CookieController
    {
        use ContainerAwareTrait;

        public function updateAction()
        {
            /** @var JsonCookie $cookie */
            $cookie = $this->container->get('project.cookie.foo');
            $cookie->setValue(['bar']);
            // Cookie has been marked as dirty and will be updated in the response.
            $cookie->setExpiresTime(2000000000);

            return new JsonResponse();
        }
    }

```<commit_msg>updated documentation<commit_after># CookiesBundle

Cookies bundle provides Symfony way to handle cookies defining them as services. This allows changing the values of the service and the bundle handles the actual creation and updates of cookies.

[![Build Status](https://travis-ci.org/ongr-io/CookiesBundle.svg?branch=master)](https://travis-ci.org/ongr-io/CookiesBundle)
[![Coverage Status](https://coveralls.io/repos/ongr-io/CookiesBundle/badge.svg?branch=master&service=github)](https://coveralls.io/github/ongr-io/CookiesBundle?branch=master)
[![Latest Stable Version](https://poser.pugx.org/ongr/cookies-bundle/v/stable)](https://packagist.org/packages/ongr/cookies-bundle)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ongr-io/CookiesBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ongr-io/CookiesBundle/?branch=master)

## Install bundle
   
To install this bundle add it to composer.

```bash

   composer require ongr/cookies-bundle

```

Then register it in `AppKernel.php`

```php

   class AppKernel extends Kernel
   {
       public function registerBundles()
       {
           return [
               // ...
               new ONGR\CookiesBundle\ONGRCookiesBundle(),
           ];
       }

       // ...
   }

```

That's it - the bundle is ready for work.

## Working with cookies

### How to define a cookie model

ONGR provides cookie model abstraction for working with cookie values in the request and response.

One can define a service:

```yaml

    parameters:
        project.cookie.foo.name: cookie_foo
        project.cookie.foo.defaults: # Defaults section is optional
            http_only: false
            expires_interval: P5DT4H # 5 days and 4 hours

    services:
        project.cookie.foo:
            class: %ongr_cookie.json.class%
            arguments: [ %project.cookie.foo.name% ]
            calls:
                - [setDefaults, [%project.cookie.foo.defaults%]] # Optional
            tags:
                - { name: ongr_cookie.cookie }

```
> `Notice` Cookie models' names of a cookie service should not contain dot symbol '.' and must be the same as cookie names that need to be modeled.

Such injected service allows accessing cookie value. If the value has been modified by your code, it will send new value back to the client browser.
Manipulating cookie values with ONGR Cookies Bundle is very easy: you need to get the cookie model service and from it you can get the cookie value
with `getValue()` method and set the value with `setValue(mixed $value)` method.

```php

    class CookieController
    {
        use ContainerAwareTrait;

        public function updateAction()
        {
            /** @var JsonCookie $cookie */
            $cookie = $this->container->get('project.cookie.foo');
            $cookie->setValue(['bar']);
            // Cookie has been marked as dirty and will be updated in the response.
            $cookie->setExpiresTime(2000000000);

            return new JsonResponse();
        }
    }

```
{'fraction_non_alphanumeric': 0.111619107677755, 'fraction_numerical': 0.005044931420463503, 'mean_word_length': 2.491469455145845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3760425', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1768, 'n_words': 487}