Report abuse

Original Way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        string myUrl = "blah";
	if (AllowPostToWordpress(myUrl)) 
  	{
		// do stuff
	}


	private static bool AllowPostToWordpress(bool siteUrlOverrideExists)
        {
            // this is verbose (the code could have been written in many fewer lines)
            // but i want it to be clear what's going on here.  there are two levels of protection 
            // against accidental posts to wordpress which are here for our dev/staging environments.
            // first, we have a key that simply says whether or not we can post to production.
            // next, we have the ability to override the xmlrpc endpoint in the database 
            // finally, we have the ability to require that the endpoint is overridden
            bool doPost = false;
	    bool enablePostingToWordpress = Config.GetAppSetting<bool>("EnablePostingToWordpress", false);
            bool requireWordpressOverride = Config.GetAppSetting<bool>("RequireWordpressOverride", true);
            if (enablePostingToWordpress)
            {
                if (!requireWordpressOverride)
                {
                    doPost = true;
                }
                else
                {
                    if (siteUrlOverrideExists)
                    {
                        doPost = true;
                    }
                }
            }
            return doPost;
        }

Testable Way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	bool enablePostingToWordpress = Config.GetAppSetting<bool>("EnablePostingToWordpress", false);
        bool requireWordpressOverride = Config.GetAppSetting<bool>("RequireWordpressOverride", true);
        string myUrl = "blah";
	if (AllowPostToWordpress(enablePostingToWordpress, requireWordpressOverride, myUrl)) 
  	{
		// do stuff
	}        

	private static bool AllowPostToWordpress(bool enablePostingToWordpress, bool requireWordpressOverride, bool siteUrlOverrideExists)
        {
            // this is verbose (the code could have been written in many fewer lines)
            // but i want it to be clear what's going on here.  there are two levels of protection 
            // against accidental posts to wordpress which are here for our dev/staging environments.
            // first, we have a key that simply says whether or not we can post to production.
            // next, we have the ability to override the xmlrpc endpoint in the database 
            // finally, we have the ability to require that the endpoint is overridden
            bool doPost = false;
            if (enablePostingToWordpress)
            {
                if (!requireWordpressOverride)
                {
                    doPost = true;
                }
                else
                {
                    if (siteUrlOverrideExists)
                    {
                        doPost = true;
                    }
                }
            }
            return doPost;
        }